8. String to Integer (atoi)
来源:互联网 发布:李鸿章杀降 知乎 编辑:程序博客网 时间:2024/06/05 14:57
public static int myAtoi(String str) { if(str == null || str.length() < 1) { return 0; } int len = str.length(); double maxDou = 0; boolean isPositive = true; int find = -1; int place1 = str.lastIndexOf('-'); int place2 = str.lastIndexOf('+'); if(place1 > place2) { isPositive = false; maxDou = Integer.MAX_VALUE*1.0 + 1; }else { maxDou = Integer.MAX_VALUE*1.0; } int resInt = 0; double tmpDou = 0; int i = 0; boolean overflag =false; for(i = 0 ;i < len; i++) { if(resInt != 0) { find =1; } char c = str.charAt(i); if(((c == '+' || c == '-') && find == -1) || (c == '0' && resInt == 0) || (c ==' ' && find == -1)) { if((c == '+' || c == '-') && find == -1) { find = 1; } continue; }else if(c >= '0' && c <= '9') { tmpDou = tmpDou*10 + char2num(c); if(tmpDou > maxDou) { resInt = isPositive?Integer.MAX_VALUE:Integer.MIN_VALUE; overflag = true; break; } resInt = resInt*10 + char2num(c); }else{ break; } } return overflag ?resInt:(isPositive?resInt:-resInt); } public static int char2num(char c) { int i=0; switch(c) { case '0' : i=0; break; case '1' : i=1; break; case '2' : i=2; break; case '3' : i=3; break; case '4' : i=4; break; case '5' : i=5; break; case '6' : i=6; break; case '7' : i=7; break; case '8' : i=8; break; case '9' : i=9; break; } return i; }
0 0
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 阿里云服务器与传统自建服务器的对比
- IOS开发-几种截屏方法
- JAVA动态代理
- iOS 如何给UIView 添加边框
- linux 管理
- 8. String to Integer (atoi)
- 上机三
- Android中通用配器的使用
- oracle知识点总结
- Fiddler——强大的过滤功能+断点调试
- 欢迎使用CSDN-markdown编辑器
- axis2 web-inf/services目录
- 上级四
- 嵌入式学习--work9 SPI-FLASH 续