Created
December 6, 2017 18:53
-
-
Save vaibhav93/39f0953f8b83d99ffdb233e87bb70f29 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) { | |
| if(str.length()==0) | |
| return 0; | |
| int sign = 1; | |
| int i =0; | |
| int base=0; | |
| while(str.charAt(i)==' ') | |
| i++; | |
| if(str.charAt(i)=='-'){ | |
| sign = -1; | |
| i++; | |
| } | |
| while(i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9'){ | |
| if(base*10>Integer.MAX_VALUE || (base*10==Integer.MAX_VALUE && str.charAt(i)-'0'>7)) | |
| { | |
| if(sign==1) | |
| return Integer.MAX_VALUE; | |
| else | |
| return Integer.MIN_VALUE; | |
| } | |
| base = base*10 + Integer.valueOf(str.charAt(i)); | |
| i++; | |
| } | |
| return base*sign; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment