Created
September 22, 2018 01:00
-
-
Save zhangxiaomu01/8aef6fb2564b9a6f8045eaa5cb92b8b2 to your computer and use it in GitHub Desktop.
This file contains 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) { | |
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