Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- class Solution {
- public int myAtoi(String str) {
- StringBuilder sb = new StringBuilder();
- int f=0;
- for(f=0;f<str.length();f++){
- if(str.charAt(f)==' '){
- continue;
- }else{
- break;
- }
- }
- if(f<str.length() && (str.charAt(f)=='+' || str.charAt(f)=='-')){
- sb.append(str.charAt(f));
- f++;
- }
- for(int i=f;i<str.length();i++){
- if((str.charAt(i)>=48 && str.charAt(i)<=57)){
- sb.append(str.charAt(i));
- continue;
- }else{
- break;
- }
- }
- String ret = sb.toString();
- if(ret.length()==0 || (ret.length()==1 && (ret.charAt(0)=='+' || ret.charAt(0)=='-'))) {
- return 0;
- }
- BigInteger res = new BigInteger(ret);
- if(res.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
- return Integer.MAX_VALUE;
- }
- if(res.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
- return Integer.MIN_VALUE;
- }
- return res.intValue();
- }
- }
Add Comment
Please, Sign In to add comment