今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习

7. Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321 Example2: x = -123, return -321

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10): Test cases had been added to test the overflow behavior.

注:颠倒整数中的每一位,符号位保留。估计会有一些极端情况需要考虑,这里指出了几个,如,末位为0 翻转之后是什么?又如,如果翻转之后超出了数据类型的表达范围怎么办?。。。可能还会遇到其它的困难^~^

解:在考虑最值的时候必须另外定义最值为int 常量,否则的话系统会给他分配一个合适的类型

直接写成这个做判断是不可以的。

if (temp >0x7fffffff || temp < 0x80000000)
{
return result;
}

下面是提交通过的代码:

class Solution {
public:
int reverse(int x) {
int result{0};
long long temp{0};
const int max_int=0x7fffffff;// 0111 1111 1111 1111 ... 1111 32bit
const int min_int=0x80000000;//1000 0000 0000 0000 ... 0000 32bit while (x != 0)
{
temp = temp*10 + x%10;
x = x / 10;
if (temp > max_int || temp < min_int)
{
return result;
}
}
result = temp;
return result;
}
};

8. String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10): The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

注:把一个字符串转换为整数。要考虑所有可能的输入情况。这个问题在《c++入门经典》中设计计算器时遇到过,只是当时没有细细的分析每一种情况,现在要好好看看。

解:这个简直是不可理喻,一下列举了一些可能出现的情况

input:“+ - 2”expected:0

input:“+ 01   1 2”expected:1

input:“+ + 2”expected:0

input:“-012a34”expected:12

input:"    +11191657170"expected:2147483647

代码如下,有待精简:

long long  result{ 0 };
int n{ 0 };//位数
int sign{ 1 }, p_n_flag{ 0 }, zero_flag{0};
for (string::iterator s_i = str.begin(); s_i != str.end(); s_i++)
{
if (*s_i == '-')
{
if (p_n_flag==0)
{
p_n_flag = 1;
sign = -1;
continue;
}
else
{
sign = 0;//如果出现两次被认为是非法输入
break;
}
}
if (*s_i == '+')
{
if (p_n_flag == 0)
{
p_n_flag = 1;
sign = 1;
continue;
}
else
{
sign = 0;
break;
}
}
if (*s_i == ' ')
{
if (n == 0 && zero_flag == 0 && p_n_flag==0)
{ continue; }
else
{break;}
}
if (*s_i == '0')
{
zero_flag = 1;
if (n == 0)
{continue;}
else
{
n = n + 1;
result = result * 10;
if (result > INT_MAX)
{ switch (sign)
{
case 0:return 0;
case 1:return INT_MAX;
case -1:return INT_MIN;
default:
break;
}
}
else
{continue;} }
}
if ((*s_i) > '0' && ((*s_i) < '9' || (*s_i) == '9'))
{
n = n + 1;
//result = result * 10 + ((*s_i) - 48);
result = result * 10 + ((*s_i) - '0');//最后一个括号里不用显式的值48 而是用‘0’
cout << result << endl;
if (result > INT_MAX)
{ switch (sign)
{
case 0:return 0;
case 1:return INT_MAX;
case -1:return INT_MIN;
default:
break;
}
}
}
else
{
break;
} }
result = result*sign; return result;

Leetcode 题目整理-2 Reverse Integer && String to Integer的更多相关文章

  1. Leetcode 题目整理-3 Palindrome Number & Roman to Integer

    9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...

  2. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  3. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

  4. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  5. LeetCode.8-字符串转整数(String to Integer (atoi))

    这是悦乐书的第349次更新,第374篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第4题(顺位题号是8).实现将字符串转换为整数的atoi方法. 该函数首先去掉所需丢 ...

  6. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. Leetcode 题目整理-8 Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

  8. Leetcode 题目整理-1

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  9. 【leetcode题目整理】数组中找子集

    368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...

随机推荐

  1. DEVOPS技术实践_22:根据参数传入条件控制执行不同stage

    前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的stage 使用叫when 和expression控制某一个stage的运行, 运行场景例如写了多个stage,这个pipeline脚 ...

  2. 洛谷$P$2472 蜥蜴 $[SCOI2007]$ 网络流

    正解:网络流 解题报告: 传送门! $umm$一看就是个最大流呗,,,就直接考虑怎么建图趴$QwQ$ 首先看到这个高度减小其实就相当于对这个点的次数有约束,就显然拆点呗,流量为高度 然后$S$连向左侧 ...

  3. $Noip2011/Luogu1311$ 选择客栈

    $Luogu$ $Sol$ 暴力十分显然叭.正解不是很好想. 我最开始想维护所有色调的客栈的前缀和后缀,然后每扫到一个最低消费合法的就统计一次答案.但是这样会重复计数,两个合法客栈之间有几个消费合法的 ...

  4. 「USACO 1.3」 Name That Number 解题报告

    \(注意 该篇题解为本人较早时期写的题解 所以会很傻 直接能用map 以string为下标偏偏要绕弯儿 有时间改一改QAQ\) [USACO1.2]Name That Number 题目描述 在威斯康 ...

  5. 1067 试密码 (20分)C语言

    当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过 20 的.不包含空格. ...

  6. 使用百度NLP接口对搜狐新闻做分类

    一.简介 本文主要是要利用百度提供的NLP接口对搜狐的新闻做分类,百度对NLP接口有提供免费的额度可以拿来练习,主要是利用了NLP里面有个文章分类的功能,可以顺便测试看看百度NLP分类做的准不准.详细 ...

  7. ASCII、UNICODE、UTF

    在计算机中,一个字节对应8位,每位可以用0或1表示,因此一个字节可以表示256种情况. ascii 美国人用了一个字节中的后7位来表达他们常用的字符,最高位一直是0,这便是ascii码. 因此asci ...

  8. 2020 年了,Java 日志框架到底哪个性能好?——技术选型篇

    大家好,之前写(shui)了两篇其他类型的文章,感觉大家反响不是很好,于是我乖乖的回来更新硬核技术文了. 经过本系列前两篇文章我们了解到日志框架大战随着 SLF4j 的一统天下而落下帷幕,但 SLF4 ...

  9. 自动将本地文件保存到GitHub

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 这篇文章主要讲讲如何自动将本地文件保存到GitH ...

  10. [Golang] 剑走偏锋 -- IoComplete ports

    前言 Golang 目前的主要應用領域還是後臺微服務,雖然在業務領域也有所應用但仍然是比較小衆的選擇.大多數的服務運行環境都是linux,而在windows中golang應用更少,而作者因爲特殊情況, ...