class Solution {
public:
int myAtoi(string str) {
const char *p = str.c_str();
int ret = 0;
int sign = 1;
while (isspace(*p)) p++; // 过滤前面的空格
if (*p == '-') // 判断字符串首部是否带有符号
{
sign = -1;
p++;
} else if (*p == '+')
{
sign = 1;
p++;
}
while (*p >= '0' && *p <= '9')
{
// 字符串转换后超出整形范围
if ( ret > (INT_MAX / 10) || (ret == INT_MAX/10 && (*p - '0') > 7) )
{
if(sign == 1)
return INT_MAX;
return INT_MIN;
}
ret = ret*10 + (*p - '0');
p++;
}
ret = sign * ret;
return ret;
}
};