class Solution {
public:
int atoi(const char *str) {
if(str == NULL) return 0;
if(*str == '\0') return 0;
long long res = 0;
//去除空格
while(*str == ' ') str++;
bool ispositive = true;
if(*str == '-'){ ispositive = false;str++;}
else if(*str == '+'){ispositive = true;str++;}
while(*str&&isdigit(*str)){
int temp = *str - '0';
res = res*10 + temp;
str++;
}
if(!ispositive)
res = -res;
if(res > INT_MAX) res = INT_MAX;
else if(res < INT_MIN) res = INT_MIN;
return res;
}
};
String to Integer (atoi)
最新推荐文章于 2014-11-13 14:15:32 发布