Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created September 22, 2018 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/8aef6fb2564b9a6f8045eaa5cb92b8b2 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/8aef6fb2564b9a6f8045eaa5cb92b8b2 to your computer and use it in GitHub Desktop.
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