leetcode-String to Integer (atoi)
class Solution {
public:
int atoi(const char *str)
{
bool negative = false;
while(*str==' ')str++ ;
if(*str=='-')
{
negative = true;
str++;
}
else if(*str=='+')
str++;
long long result = 0;
while(*str!='\0')
{
if(*str>='0'&&*str<='9')
{
result = result * 10 + *str - '0';
str++;
}
else
break;
}
result = negative?-result:result;
if(result>INT_MAX)
return INT_MAX;
else if(result<INT_MIN)
return INT_MIN;
return result;
}
};
考虑不周,错了好多次
1. +-2 --2 返回0
2. - 00023ea9返回 -23
3. 超过范围的
每天早上叫醒你的不是闹钟,而是心中的梦~

浙公网安备 33010602011771号