3 step solution in java


  • 0
    public class Solution {
        public int myAtoi(String str) {
          if (str==null || str.length() == 0){
                return 0;
            }
            int num = 0;
            str = str.trim();
            boolean isPositive = true;
    
            int len = str.length();
            for (int i = 0; i < len; i++){
                char temp = str.charAt(i);
    
                //1.settle sign
                if (i==0) {
                    if (temp == '-') {
                        isPositive = false;
                        continue;
                    } else if (temp == '+') {
                        isPositive = true;
                        continue;
                    }
                }
    
                //2.settle invalid char
                if (temp<48||temp>57){
                    return isPositive ? num : -num;
                }
    
                if (Integer.MAX_VALUE / 10 < num | (Integer.MAX_VALUE / 10 == num & Integer.MAX_VALUE % 10 < (temp-'0'))) {
                    return isPositive ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                }else {
                    num = (num * 10 + (temp - '0'));
                }
            }
            return isPositive ? num : -num;
        }
    }
    

Log in to reply
 

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