https://leetcode.com/problems/string-to-integer-atoi/discuss/
实现 atoi 函数
思路:
循环按位解析,需要手动判断溢出等情况,写的有点乱,之后再精简下.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class Solution { public int myAtoi(String str) { str = str.trim(); if(str.length() == 0){ return 0; } int number=0; int fu=1; int startIndex=0; if(str.charAt(0)=='-'){ fu=-1; startIndex=1; }else if(str.charAt(0)=='+'){ startIndex=1; } for(int i=startIndex;i<str.length();i++){ char c=str.charAt(i); if(isNumber(c)){ int n=c-'0'; if(fu==1 && ( number*10>=(Integer.MAX_VALUE-n) || number>Integer.MAX_VALUE/10 )){ number=Integer.MAX_VALUE; return number; }else if(fu==-1 && (-10*number<=(Integer.MIN_VALUE+n)|| number>Integer.MIN_VALUE/-10 )){ number=Integer.MIN_VALUE; return number; } number=number*10+n; }else{ break; } } return fu*number; }
private boolean isNumber(char c){ return c>='0'&&c<='9'; } }
|
v1.5.2