【题目描述】
【思路 】
模拟
class Solution {
public int strToInt(String str) {
if( str == null || str.length() == 0) return 0;
int k = 0, n = str.length();
char c [] = str.toCharArray();
//处理空格
while( k < n && c[k] == ' ') k ++;
//处理数字前的正负号
boolean is_negative = false;
if( k < n && c[k] == '+') k ++;
else if( c[k] == '-'){
k ++;
is_negative = true;
}
long ans = 0;
//开始处理数字
while(k < n && c[k] >= '0' && c[k] <= '9'){
ans = ans * 10 + (c[k] - '0');
k ++;
}
//负数
if( is_negative) ans = - ans;
if( ans > Integer.MAX_VALUE) return Integer.MAX_VALUE;
if( ans < Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int)ans;
}
}