class Solution {
public:
int myAtoi(string str) {
int index=0,sign=1,total=0;
//empty string
if(str.length()==0) return 0;
//remove blank
while(str[index]==' ' && index<str.length()) index++;
//sign
if(str[index]=='+' || str[index]=='-')
sign=(str[index++]=='+')?1:-1;
//convert number
while(index < str.length() && str[index] >= '0' && str[index] <= '9'){
if(total>INT_MAX/10 || (total==INT_MAX/10 && str[index] - '0' > INT_MAX%10))
return (sign==1)?INT_MAX:INT_MIN;
total=total*10+str[index++] - '0';
}
return total*sign;
}
};
LeetCode Day7 String_to_integer
猜你喜欢
转载自blog.csdn.net/weixin_41394379/article/details/82957361
今日推荐
周排行