LeetCode-8.String to Integer (atoi)
来源:互联网 发布:c语言中求绝对值 编辑:程序博客网 时间:2024/06/03 15:11
https://leetcode.com/problems/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.
spoilers alert... click to show 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.
错误代码
int myAtoi(string str){int n = str.length();if (n == 0) return 0;int i = 0, res = 0, sign=1;while (str[i] == ' ') i++;if (i == n) return 0;if(str[i] == '+'|| str[i] == '-')sign = str[i++] == '+' ? 1 : -1;while (str[i] >= '0'&&str[i] <= '9'){if (sign == 1 && res > (INT_MAX - (str[i] - '0')) / 10)return INT_MAX;if (sign == -1 && res > -(INT_MIN + (str[i] - '0')) / 10)return INT_MIN;res = res * 10 + (str[i++] - '0');}return res*sign;}
分析:当str[i]='0'时,-(INT_MIN + (str[i] - '0'))越界,因此在取反前还要一层括号
int myAtoi(string str) { int n = str.length(); if (n == 0) return 0; int i = 0, res = 0, sign=1; while (str[i] == ' ') i++; if (i == n) return 0; if(str[i] == '+'|| str[i] == '-') sign = str[i++] == '+' ? 1 : -1; while (str[i] >= '0'&&str[i] <= '9') { if (sign == 1 && res > (INT_MAX - (str[i] - '0')) / 10) return INT_MAX; if (sign == -1 && res > -((INT_MIN + (str[i] - '0')) / 10)) return INT_MIN; res = res * 10 + (str[i++] - '0'); } return res*sign; }
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- LeetCode 8.String to Integer (atoi)
- [Leetcode] 8. String to Integer (atoi)
- [leetcode] 8.String to Integer (atoi)
- [LeetCode] 8.String to Integer (atoi)
- <LeetCode OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- Tomcat 虚拟目录配置or Tomcat软连接如何建立?
- Oracle学习笔记(三)
- iOS 获取图片路径的后缀/获取字符串的后缀
- SVN使用教程总结
- 使用MyBatis(八)一对一关联(嵌套查询,嵌套结果)
- LeetCode-8.String to Integer (atoi)
- xml 转json 传输
- Math.ceil
- Android--Retrofit浅入深出
- Android传递Parcelable时忽略的一个细节
- python中list的拷贝与numpy的array的拷贝
- 类的封装
- 你真的了解软件测试行业吗?
- angularJs三种依赖注入方式与minify