java solution - Easy to understand at 1st read with explanations!!


  • 0
    L
    public class Solution {
        public int myAtoi(String str) {
            if(str == null || str.length()<1) return 0;
            // trim white spaces
            str = str.trim();
            
            // check negative or positive
            char flag = '+'; 
            int i=0;
            if(str.charAt(0)=='-'){
                flag = '-';
                i++;
            }
            else if(str.charAt(0)=='+')
                i++;
            
            // Store Result
            double res = 0;
            while(i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'){
                res = res * 10 + (str.charAt(i)-'0');
                i++;
            }
            
            if(flag == '-')
                res = -res;
            
            // handle max and min
            if(res>Integer.MAX_VALUE) return Integer.MAX_VALUE;
            if(res<Integer.MIN_VALUE) return Integer.MIN_VALUE;
            
            return (int)res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.