LeetCode 8 String to Integer (atoi)
2015-07-22 15:48
281 查看
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.
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.
解题思路:
这道题的根本方法是借用上一道题的思路,迭代计算。
但要注意几个特殊的测试用例:
in:-0012a42 out: 12in:+-2 out:0
in: " -12003300612" out: -2147483648
in:2147483648 out:2147483647
这里要考虑最大正整数和最大负整数之间的区别,int型最大正整数为2147483647,而最大负整数为 -2147483648
这是在代码逻辑上要考虑的。
代码如下:
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; }
相关文章推荐
- 初次使用keepalived应该注意的
- cocoapods的各种坑
- 百度地图2.4.1SDK监听覆盖物的点击事件
- python getopt使用
- 分布式网站架构后续:zookeeper技术浅析
- ZooKeeper监控
- 20150722---点击按钮使指定的控件可见部分平移(JS)
- nginx配置多个虚拟主机,只有其中某个有作用
- Antx简介(ali_PPT)
- drools入门(二)-----规则引擎Drools解决汽水问题(复杂逻辑)
- ant.xml文件
- JAVA之ConcurrentHashMap源码深度分析
- Day02笔记
- 二叉树的前序、中序、后续遍历,递归、非递归实现
- C语言inline函数
- HDU 3555 Bomb (简单数位DP)
- Extjs4 源码分析系列一 类的创建过程
- AICL(Auto Input Current Limited)
- QListWidget实现图片缩略图形式的列表
- 一般题模板