LeetCode----8. String to Integer (atoi)
来源:互联网 发布:淘宝甩手铺货 编辑:程序博客网 时间:2024/06/07 17:28
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.
Subscribe to see which companies asked this question
Runtime Error Message:Line 9: java.lang.NumberFormatException: For input string: "+"
2.超出最大范围打印边界值,而非0;3.存在空格
4.+ -开头的符号
public class Solution {
public int myAtoi(String str) {
try{
if(str==null||str.length()==0){return 0;}
str=str.trim();
int m=Integer.parseInt(str);
if(Integer.parseInt(str)>Integer.MAX_VALUE){return Integer.MAX_VALUE;}
if(Integer.parseInt(str)<Integer.MIN_VALUE){return Integer.MIN_VALUE;}
return m;
}catch(Exception e){
}
return 0;
}
}
public int myAtoi(String str) {
try{
if(str==null||str.length()==0){return 0;}
str=str.trim();
int m=Integer.parseInt(str);
if(Integer.parseInt(str)>Integer.MAX_VALUE){return Integer.MAX_VALUE;}
if(Integer.parseInt(str)<Integer.MIN_VALUE){return Integer.MIN_VALUE;}
return m;
}catch(Exception e){
}
return 0;
}
}
Input:" -0012a42"
Output:0
Expected:-12
public int atoi(String str) {if (str == null || str.length() < 1)return 0; // trim white spacesstr = str.trim(); char flag = '+'; // check negative or positiveint i = 0;if (str.charAt(0) == '-') {flag = '-';i++;} else if (str.charAt(0) == '+') {i++;}// use double to store resultdouble result = 0; // calculate valuewhile (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {result = result * 10 + (str.charAt(i) - '0');i++;} if (flag == '-')result = -result; // handle max and minif (result > Integer.MAX_VALUE)return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE)return Integer.MIN_VALUE; return (int) result;}
0 0
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- 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 OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- HDU 5879 Cure
- Presto源码分析(ScanFilterAndProjectOperator)
- Linux环境进程间通信(五): 共享内存(下)
- java内存模型学习
- 第三周项目3--求集合并集
- LeetCode----8. String to Integer (atoi)
- STCT: Sequentially Training Convolutional Networks for visual tracking
- Codeforces-714A-Meeting of Old Friends
- bitmap 图片优化
- JSP与Servlet的跳转及得到路径方法整理
- JavaScript原型、闭包、继承和原型链等等总结
- PAT乙级训练题--1001
- 绘制地图&热力图-R语言
- 2016秋季练习