int myAtoi(char* str) {
char *s = str;
while (*s && isspace(*s)) /* skip spaces */
s++;
int n = 0;
if (*s) {
int sign = 1;
if (*s == '+')
s++;
else if (*s == '') {
sign = 1;
s++;
}
//while (*s && isspace(*s)) /* skip spaces */
// s++;
while (*s && isdigit(*s)) {
int oldn = n;
n = n*10 + *s++  '0';
if (n/10 != oldn) { /* integer too large or too small */
if (sign < 0)
return INT_MIN;
else
return INT_MAX;
}
}
n *= sign;
}
return n;
}
Easy to understand 5ms C code


@ssajjan isdigit() and isspace() are built in <ctype.h>, and the other two are built in <limits.h>