《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
来源:互联网 发布:js把URL转换为对象 编辑:程序博客网 时间:2024/06/18 13:39
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
008. String to Integer (atoi) [E]
题目
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.
思路
这题也比较好做,关键是要考虑挺多东西,我也是提交了好多次才发现有这么多要考虑的地方。
- 开头的空格
- 正负符号的处理
- 溢出处理
- 非法输入
开头空格处理:
while(str[i] == " ") i++;
正负号的处理:我觉得yuruofeifei这个解决方案简直赞
if (str[i] == '-' || str[i] == '+') { sign = 1 - 2 * (str[i++] == '-'); } …… return base * sign;
溢出处理(可以参考上一道题):
if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > INT_MAX%10)) { if (sign == 1) return INT_MAX; else return INT_MIN; }
非法输入:其实只用过滤就行了
while (str[i] >= '0' && str[i] <= '9') { …… }
代码
我的代码,不够简洁,可以参考yuruofeifei的代码,在下面
class Solution {public: int myAtoi(string str) { long tmp=0; bool neg; int i = 0; while(str[i] == ' ') i++; //读掉空格 neg = str[i] == '-'?1:0; for(i = i+ (neg || str[i] == '+');i < str.length();i++) //如果是- 或 + i+1跳过符号 { if(str[i] - '0' >= 0 && str[i] - '0' < 10) //过滤非法输入 { tmp *= 10; tmp += (str[i] - '0'); if(tmp >= INT_MAX && !neg) //溢出判断 { tmp = INT_MAX; break; } if(tmp -1 >= INT_MAX && neg) //除了符号,INT_MAX和INT_MIN只差1 { tmp = INT_MIN; break; } } else break; } if(neg) return -tmp; return tmp; }};
yuruofeifei的代码
int myAtoi(string str) { int sign = 1, base = 0, i = 0; while (str[i] == ' ') { i++; } if (str[i] == '-' || str[i] == '+') { sign = 1 - 2 * (str[i++] == '-'); } while (str[i] >= '0' && str[i] <= '9') { if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) { if (sign == 1) return INT_MAX; else return INT_MIN; } base = 10 * base + (str[i++] - '0'); } return base * sign;}
0 0
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
- LeetCode题解——8 String to Integer (atoi)
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
- LeetCode题解-8-String to Integer (atoi)
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
- LeetCode 8. String to Integer (atoi) C++ --字符串转为数字,包含正负号、空格、字母、数字等字符
- LeetCode题解:String to Integer (atoi)
- LeetCode - String to Integer (atoi) 题解
- LeetCode题解:String to Integer (atoi)
- LeetCode题解--8. String to Integer (atoi)
- Leetcode题解 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode题解
- leetcode题解-8. String to Integer (atoi)
- leetcode(E) string to integer(atoi)
- LEETCODE 8 String to Integer (atoi) (JAVA题解)
- LeetCode——String to Integer (atoi)
- LeetCode——String to Integer (atoi)
- String to Integer (atoi) —— Leetcode
- 共同学习Java源码--常用数据类型--String(十三)
- 【GoogleCodeJam Qualification Round 2016】Problem A. Counting Sheep
- tomcat的war包手工部署和自动部署Windows8
- 提取多层嵌套JSON类型数据
- 转载:Java的异常处理机制:try、catch、finally、throw、throws
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
- dual表
- Calling C and C++ from IDL (一)
- nmon结果分析
- mapreduce(JAVA)实现(大数据)电话号码对应的流量排序(倒序)
- 一道古老的分赃题之我见
- 【机房合作】——总结
- JavaScript实现简单人机五子棋
- 集合之Vector