Created
November 12, 2019 11:23
-
-
Save pascaljp/13b152dd76128a6f814c106a79ce8950 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int myAtoi(string str) { | |
int pos = 0; | |
while (str[pos] == ' ' && str[pos] != NULL) { | |
pos++; | |
} | |
if (str[pos] == NULL) { | |
return 0; | |
} | |
char sign = '+'; | |
if (str[pos] == '+' || str[pos] == '-') { | |
sign = str[pos]; | |
pos++; | |
} | |
long long result = 0; | |
while ('0' <= str[pos] && str[pos] <= '9') { | |
result = result * 10 + (str[pos] - '0'); | |
if (result > INT_MAX) { | |
return sign == '+' ? INT_MAX : INT_MIN; | |
} | |
pos++; | |
} | |
return sign == '+' ? result : -result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment