Skip to content

Instantly share code, notes, and snippets.

@vaibhav93
Created December 6, 2017 18:53
Show Gist options
  • Select an option

  • Save vaibhav93/39f0953f8b83d99ffdb233e87bb70f29 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhav93/39f0953f8b83d99ffdb233e87bb70f29 to your computer and use it in GitHub Desktop.
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