String to Integer (atoi) leetcode java
来源:互联网 发布:全国省市县数据库 sql 编辑:程序博客网 时间:2024/05/29 07:52
原文地址:http://www.cnblogs.com/springfor/p/3896499.html
题目:
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.
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.
题解:
这题主要就是考虑一下corner case。
越界问题?
正负号问题?
空格问题?
精度问题?
代码如下:
2 if (str == null || str.length() < 1)
3 return 0;
4
5 // trim white spaces
6 str = str.trim();
7
8 char flag = '+';
9
10 // check negative or positive
11 int i = 0;
12 if (str.charAt(0) == '-') {
13 flag = '-';
14 i++;
15 } else if (str.charAt(0) == '+') {
16 i++;
17 }
18 // use double to store result
19 double result = 0;
20
21 // calculate value
22 while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
23 result = result * 10 + (str.charAt(i) - '0');
24 i++;
25 }
26
27 if (flag == '-')
28 result = -result;
29
30 // handle max and min
31 if (result > Integer.MAX_VALUE)
32 return Integer.MAX_VALUE;
33
34 if (result < Integer.MIN_VALUE)
35 return Integer.MIN_VALUE;
36
37 return (int) result;
38 }
更新:
因为遇见过了atol,string to long 这样的问题,就不能用这种比当前数据类型长的方法解决。
另一种方法是更加普遍和巧妙的,就是用这样的判断:
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相等,那么就判断当前遍历的元素值跟最大值的最后一位的大小关系即可
代码如下:
2 if (str == null || str.length() < 1)
3 return 0;
4
5 // trim white spaces at beginning and end
6 str = str.trim();
7
8 char flag = '+';
9
10 // check negative or positive
11 int i = 0;
12 if (str.charAt(0) == '-') {
13 flag = '-';
14 i++;
15 } else if (str.charAt(0) == '+') {
16 i++;
17 }
18 // use double to store result
19 int result = 0;
20
21 // calculate value
22 while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
23 if(Integer.MAX_VALUE/10 < result || (Integer.MAX_VALUE/10 == result && Integer.MAX_VALUE%10 < (str.charAt(i) - '0')))
24 return flag == '-' ? Integer.MIN_VALUE : Integer.MAX_VALUE;
25
26 result = result * 10 + (str.charAt(i) - '0');
27 i++;
28 }
29
30 if (flag == '-')
31 result = -result;
32
33 return result;
34 }
Reference: http://www.programcreek.com/2012/12/leetcode-string-to-integer-atoi/
- [Leetcode] String to Integer (atoi) (Java)
- LeetCode – String to Integer (atoi) (Java)
- 【Leetcode】String to Integer(atoi) in JAVA
- [LeetCode Java] 8 String to Integer (atoi)
- [LeetCode][Java] String to Integer (atoi)
- [leetcode-8]String to Integer (atoi)(java)
- LeetCode : String to Integer (atoi) [java]
- Leetcode: 8. String to Integer (atoi)(JAVA)
- LeetCode(java)8. String to Integer (atoi)
- [LeetCode-Java]8. String to Integer (atoi)
- [LeetCode] 8. String to Integer (atoi) java
- leetcode String to Integer (atoi)(Java)
- String to Integer (atoi) leetcode java
- [leetcode]8. String to Integer (atoi)(Java)
- 【LeetCode】String to Integer (atoi)(java)
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- Linux安装Anaconda 后使用Anconda自带的python版本
- angular jit and aot
- java中的运算符和表达式详解
- localtime与localtime_r
- Qt学习——Qt下载和安装
- String to Integer (atoi) leetcode java
- 100项PPT制作技术,做出让观众倾倒的演示文档!
- Springmvc配置
- Leetcode题解
- 详解Android Activity:Activity的生命周期
- JDBC(Java DataBase Connectivity)增删改查简单整理
- Linux下对NS2项目的仿真分析全过程代码(从tcl脚本-awk文本处理-gnuplot)
- 【剑指offer】题39:平衡二叉树
- 关于Linux下编译和使用动态链接库静态链接库