my java solution ,hope can help someone


  • 0
    L
    public int myAtoi(String str) {
        if(str == null || "".equals(str.trim())) return 0;
        str = str.trim();
        if(str.matches("[+-]{0,1}\\d+")){
            return new Double(str).intValue();
        }
        //use double because double is larger than int and long
        double sum = 0;
        //count the '+'
        int addCount = 0;
        //count the '-';
        int  subCount = 0;
        char[] numbers = str.toCharArray();
        for(char num : numbers){
            if('+' == num){
                addCount++;
            }else if('-' == num){
                subCount++;
            }
            //it means that it is not a digit because A-Z[65-90] a-z[97,122] and 0-9[48-58]
            else if(num - '0' > 9){
                break;
            }
            else if(' ' == num){
                break;
            }else{
                sum *= 10;
                sum += num - '0';
            }
            //it is not a correct format 
            if((addCount + subCount) >= 2){
                return 0;
            }
        }
        if(sum > Integer.MAX_VALUE){
            if(subCount == 0){
                return Integer.MAX_VALUE;
            }
            return Integer.MIN_VALUE;
        }else{
             if(subCount == 0){
                 return (int)sum;
             }
             return (int)sum * -1;
        }
    }

Log in to reply
 

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