String转int
来源:互联网 发布:淘宝鞋店知乎 编辑:程序博客网 时间:2024/05/29 17:39
题目:将String转int
本身为简单题,但是其中的细节非常多,可以用来考测试。
需要注意的有:
1、String中可能有非数字的符号
2、String中有征服号
3、int越界
public class Solution { public int atoi(String str) { Integer num = null; int index = 0; char[] c = str.trim().toCharArray(); if (c.length==0) return 0; String s = ""; for (int i=0;i<c.length;i++){ if ((c[i] == '-' || c[i] == '+' )&&index==0){ s += c[i]; index++; } else if ((c[i] == '-' || c[i] == '+' )&&index!=0) break; else if (c[i]-'0'>-1 && c[i]-'9'<1) s += c[i]; else break; } if (s.length()==1&&(s.startsWith("+")||s.startsWith("-")) || s.length()==0) return 0; try{ num = new Integer(s); }catch (NumberFormatException e){ if (s.startsWith("-")) return Integer.MIN_VALUE; else return Integer.MAX_VALUE; } return num; }}
本身为简单题,但是其中的细节非常多,可以用来考测试。
需要注意的有:
1、String中可能有非数字的符号
2、String中有征服号
3、int越界
4、String为空
0 0
- [转]int转string string转int
- int转string string转int
- c# int 转 string string 转 int
- int转string,string转int
- c++ string转int,int转string
- int转string/ string 转Int
- String转int转String
- C++ int转string string转int int转二进制
- String 转 int
- string 转 int 函数
- int 转 string
- string 转 int
- string 转 int
- int转string
- int 转 string
- int转string
- String 转int
- c++ int 转 string
- python脚本中应用模块的方式总结
- (HDUStep 1.2.2)hide handkerchief(用辗转相除法来求最大公约数)
- MFC CFileDialog实现源码
- abort函数和exit函数
- 【三层程序设计】——总结
- String转int
- go语言写日志
- VS2008编译错误fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
- svn
- 反编译apk
- mybatis打印SQL,并显示参数
- 声母数字助记系统
- uvaoj 12169 Disgruntled Judge 扩展欧几里得算法
- 排序算法(四)- 希尔排序