Skip to content

Instantly share code, notes, and snippets.

@pascaljp
Created November 12, 2019 11:23
Show Gist options
  • Save pascaljp/13b152dd76128a6f814c106a79ce8950 to your computer and use it in GitHub Desktop.
Save pascaljp/13b152dd76128a6f814c106a79ce8950 to your computer and use it in GitHub Desktop.
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