代码实现:
int myAtoi(char *str) {long ret = 0;int flag = 1; // 默认正数// 去除空格及判断符号位while (*str == ' ') {str++;}if (*str == '-') {flag = -1;str++;} else if (*str == '+') {str++;}// 排除非数字的情况if (*str < '0' || *str > '9') {return 0;}while (*str >= '0' && *str <= '9') {ret = ret * 10 + (*str - '0');// 判断溢出if ((int)ret != ret) {return (flag == 1) ? (INT_MAX) : (INT_MIN);}str++;}ret *= flag;return (int)ret; }