String to Integer (atoi)
来源:互联网 发布:制作cg动画软件 编辑:程序博客网 时间:2024/05/13 17:56
ref http://www.cnblogs.com/springfor/p/3896499.html
public class Solution {
public int atoi(String str) {if(str==null || str.length()<=0) return 0;
str = str.trim();
int i=0, flag = 1;
if(str.charAt(i)=='-') {
flag=-1;
i++;
}else if(str.charAt(i)=='+') {
i++;
} // 注意这里如果不处理第一位,则会把 +/- 当成数字被处理
int res = 0;
while(i<str.length() && str.charAt(i) >='0' && str.charAt(i) <='9'){
if( Integer.MAX_VALUE/10<res || (Integer.MAX_VALUE/10== res && Integer.MAX_VALUE%10 < str.charAt(i)-'0'))
return flag<0? Integer.MIN_VALUE: Integer.MAX_VALUE;
res = res*10 + str.charAt(i)-'0';
i++;
}
return flag <0? -res : res;
}
}
0 0
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- string to integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer(atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- 编程之美读书笔记3.1—字符串移位包含的问题
- 巴菲特致股东的一封信:1984年
- Android下免Root权限截屏
- java session共享
- 【编程之旅】关于传输剩余时间估算那些事儿
- String to Integer (atoi)
- 8.struts2_OGNL
- VS2008中"fatal error C1083:无法打开包括文件:“Stdafx.h”: No such file or directory
- PAT B1004 成绩排名
- 关于对于一个浮动图片的控制
- 巴菲特致股东的一封信:1983年
- 实战篇:如何建设企业的营销管理和分析平台
- STARTUP.A51 文件分析
- 朴素贝叶斯分类器