LeetCode 8 String to Integer (atoi)
来源:互联网 发布:淘宝爱爱丸小虎牙模特 编辑:程序博客网 时间:2024/04/18 03:31
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.
这道题的根本方法是借用上一道题的思路,迭代计算。
但要注意几个特殊的测试用例:
public int myAtoi(String str){ int len = str.length(); int re = 0; int i = 0; int j=0; int type =0; if(str.equals("")){ return 0; } while(str.charAt(j)==' '){ j++; len--; } str = str.substring(j); if(str.charAt(0)=='-'){ str= str.substring(1,len); type=1; len--; }else if(str.charAt(0)=='+'){ str= str.substring(1,len); type=0; len--; } while(i<len){ if(str.charAt(i)> 57||str.charAt(i)<48){ if(type ==1){ re = -re; } return re; } if(re==Integer.MAX_VALUE/10){ if(type ==1){ int k = 8-(str.charAt(str.length()-1)-'0'); if(k<0){ return Integer.MIN_VALUE; } return Integer.MIN_VALUE+k; }else{ int k = 7 - (str.charAt(str.length()-1)-'0'); if(k==-1){ return Integer.MAX_VALUE; } return Integer.MAX_VALUE-k; } } if (re>Integer.MAX_VALUE/10) { if(type ==1){ return Integer.MIN_VALUE; } return Integer.MAX_VALUE;} re = re*10+str.charAt(i)-'0'; i++; } if(type ==1){ re = -re; } return re; }
- LeetCode 8 - String to Integer (atoi)
- [LeetCode 8] String to Integer (atoi)
- leetcode 8STRING 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 8:《String to Integer (atoi) 》
- leetcode.8---------------String to Integer (atoi)
- leetCode #8 String to Integer (atoi)
- [LeetCode Java] 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 8 - String to Integer (atoi)
- leetcode #8 String to Integer (atoi)
- String to Integer (atoi) - LeetCode 8
- JAVA之ConcurrentHashMap源码深度分析
- ant.xml文件
- C# 属性和字段 get set
- 分布式网站架构后续:zookeeper技术浅析
- drools入门(二)-----规则引擎Drools解决汽水问题(复杂逻辑)
- LeetCode 8 String to Integer (atoi)
- python getopt使用
- ALM损坏后的恢复步骤
- 初次使用keepalived应该注意的
- ListView.addHeadView添加ViewPager,ViewPager左右切换影响ListView上下滑动
- PHP生成静态页面详细教程
- cocoapods的各种坑
- linux cooked capture
- 百度地图2.4.1SDK监听覆盖物的点击事件