Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
int myAtoi(string str) {
long temp = 0;
int i = 0, sign = 1;
while(str[i] == ' ')
{
i++;
}
if(str[i] == '-'){
sign = -1;
i++;
}
else if(str[i] == '+')
i++;
while(str[i] - '0' >=0 && str[i] - '0' <=9)
{
temp = temp*10 + (str[i] - '0');
if(sign>0 && temp>=INT_MAX)
return INT_MAX;
if(sign<0 && -temp <=INT_MIN)
return INT_MIN;
i++;
}
return temp*sign;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment