您的位置:首页 > 其它

LeeCode String to Integer (atoi)

2017-08-29 17:24 274 查看
class Solution {
public int myAtoi(String str) {
str = str.trim();
if(str == null || str.length() == 0)
return 0;
int length = str.length();
int isNegative =1;
int i = 0;
long  result = 0;
if(str.charAt(0) == '+')
i++;
else if(str.charAt(0) == '-'){
isNegative = -1;
i++;
}
for(; i<length ; i++){
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
result = result * 10 + str.charAt(i) - '0';
if(result > Integer.MAX_VALUE){
return isNegative < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
}else{
break;
}
}
return (int)result * isNegative;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: