《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
2016-04-10 11:32
417 查看
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
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.
开头的空格
正负符号的处理
溢出处理
非法输入
开头空格处理:
正负号的处理:我觉得yuruofeifei这个解决方案简直赞
溢出处理(可以参考上一道题):
非法输入:其实只用过滤就行了
yuruofeifei的代码
书的地址: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; }
相关文章推荐
- PHP_D4_“简易聊天室 ”的具体技术实现
- 随便取10 个数4
- Problem 1608 - Calculation 【状压dp】
- 目标特征检测之ORB
- hdu 5660 jrMz and angles【暴力枚举】【水题】
- 提取多层嵌套JSON类型数据
- Problem 1603 - Minimum Sum 【数学】
- 鹿伟伟的博客
- 重写 重载 隐藏
- 共同学习Java源码--常用数据类型--String(十三)
- Stanford ex1: Linear Regression
- 正则表达式
- 框架设计
- vim常用技巧
- [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)
- http2
- VMware Workstation装Mac OS X出现无法恢复错误vcpu-0
- 关于javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional
- 混合专家系统(Mixture of experts)
- leetcode之Binary Tree Level Order Traverse