<leetcode>String to Integer (atoi)
来源:互联网 发布:淘宝账单生成器手机版 编辑:程序博客网 时间:2024/06/14 00:51
问题描述:
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.
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.
解法:
问题很简单,但是要注意一下四点。
1.字符前可能会出现空格
2.整数后可跟非数字字符, “-12a78” 为“-12”
3.越界返回边界值
num = num * 10 + bit
int myAtoi(char* str) { //考虑负数,越界情况,输入空long num = 0;bool flag = true;while (*str == ' ')str++;if (*str == '-'){flag = false; str++;}else if (*str == '+'){flag = true;str++;}while (*str != '\0'){int bit = *str - '0';if (bit > 9 || bit < 0)break;num = num * 10 + bit;if (num > 2147483648 && flag == false)return -2147483648;if (num > 2147483647 && flag == true)return 2147483647;str++;}if (flag)return num;elsereturn 0 - num;}
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- 如果你胆敢import某个.m文件
- qt 登陆界面账号和密码的验证
- apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
- Qt编写自定义插件
- 通过Java访问指定url
- <leetcode>String to Integer (atoi)
- Linux slab分配器
- nexus 手机&平板电脑
- 《鸡啄米C++编程入门系列》系列技术文章整理收藏
- iOS7计算UILabel中字符串的高度
- 在Parser Generater中, 联合使用lex和yacc时, 使用文件IO输入输出
- 虚拟机中的linux系统如何上网
- 字符串的排列
- Extjs中DatePicker宽度异常解决方法