Leetcode String to Integer (atoi)
2015-09-10 01:36
274 查看
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.
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)
正负号问题?(第一位是'+' or '-' or ''0-'9')
空格问题?(用trim)
有一种方法判断是否溢出普遍且巧妙,用这样的判断:
1. Integer.MAX_VALUE/10 < result; //当前转换结果比Integer中最大值/10还大(因为这个判断放在while循环最开始,之后还要对result进行*10+当前遍历元素的操作,所以如果还乘10的result已经比Integer.MAX_VALUE/10还大,可想而知,乘了10更大)
2. Integer.MAX_VALUE/10 == result && Integer.MAX_VALUE%10 <(str.charAt(i) - '0') //另外的情况就是,当前result恰跟 Integer.MAX_VALUE/10相等,那么就判断当前遍历的元素值跟最大值的最后一位的大小关系即可
本题只要稍有不慎就极容易出错,注意:"12abc" convert to int is '12',即本题思路从最左边不是空格的位开始往右,遇到的数字都有效,直到遇到不是数字的停止。
因此代码中用result = result * 10 + (str.charAt(i) - '0')
Java code
Reference:
1. http://www.cnblogs.com/springfor/p/3896499.html
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.
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)
正负号问题?(第一位是'+' or '-' or ''0-'9')
空格问题?(用trim)
有一种方法判断是否溢出普遍且巧妙,用这样的判断:
1. Integer.MAX_VALUE/10 < result; //当前转换结果比Integer中最大值/10还大(因为这个判断放在while循环最开始,之后还要对result进行*10+当前遍历元素的操作,所以如果还乘10的result已经比Integer.MAX_VALUE/10还大,可想而知,乘了10更大)
2. Integer.MAX_VALUE/10 == result && Integer.MAX_VALUE%10 <(str.charAt(i) - '0') //另外的情况就是,当前result恰跟 Integer.MAX_VALUE/10相等,那么就判断当前遍历的元素值跟最大值的最后一位的大小关系即可
本题只要稍有不慎就极容易出错,注意:"12abc" convert to int is '12',即本题思路从最左边不是空格的位开始往右,遇到的数字都有效,直到遇到不是数字的停止。
因此代码中用result = result * 10 + (str.charAt(i) - '0')
Java code
public int myAtoi(String str) { if(str == null || str.length() < 1) { return 0; } str = str.trim(); //trim white spaces at beginning and end char flag = '+'; //check negative or positive int i = 0; if(str.charAt(0)== '-') { flag = '-'; i++; }else if(str.charAt(0)== '+'){ i++; } int result = 0; //calculate value while(str.length() > i && str.charAt(i)>='0' && str.charAt(i)<='9') { if(Integer.MAX_VALUE/10 < result || (Integer.MAX_VALUE/10 == result && Integer.MAX_VALUE%10 < (str.charAt(i)-'0'))){ return flag == '-'? Integer.MIN_VALUE: Integer.MAX_VALUE; } result = result * 10 + (str.charAt(i)-'0'); i++; } return flag=='-'? -result: result; }
Reference:
1. http://www.cnblogs.com/springfor/p/3896499.html
相关文章推荐
- SqlServer 2012 序列号
- php 5.6获取mongodb实例
- 如何优雅高效的写博客 sublime+markdown完美结合
- Android http连接设置超时无效
- ACM-括号匹配问题
- Android中通过Messenger与Service实现进程间双向通信
- 算法与数据结构之选择排序(C语言)
- php 5.6连接mongodb 数据库
- 初步15-能对齐的数据
- sql优化
- Android:复制Assets下所有的文件(遍历每一个文件夹),并存入sdcard里面
- 坚持不一定胜利,但不坚持你连胜利的机会都没有!
- 初步14-字母及编码
- CCNode解析
- 内连接、左外连接、右外连接、交叉连接区别
- 黑马程序员 Hashtable和HashMap的不同
- 《煎饼侠》大鹏为什么能请到这么多明星?
- 黑马程序员 多态
- 大鹏背景,大鹏为什么能邀请那么多明星?揭秘董成鹏的成名路
- Android学习系列(-): 电话拨号器 - 4种点击事件写法