Leetcode: String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

public class Solution {
    public int atoi(String str) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList<Integer> validBits = new ArrayList<Integer>();
        int sign=0;
        if(str==null||str.length()==0){
            return 0;
        }
        int index = 0;
        while(str.charAt(index)==' '){
            index++;
        }
        if(!(isNum(str, index) || str.charAt(index)=='+' || str.charAt(index)=='-')){
            return 0;
        }
        if(str.charAt(index)=='-'){
            sign = -1;
            index++;
        }else{
            sign = 1;
            if(str.charAt(index)=='+'){
                index++;
            }
        }
        //now find the first non-zero bit
        while(str.charAt(index)=='0'){
            index++;
        }
        while(index<str.length() && isNum(str,index)){
            int asc = (int)str.charAt(index);
            int bit = asc-'0';
            validBits.add(bit);
            index++;
        }

        int length = validBits.size();
        if(length==0){
            return 0;
        }

        double sum =0;
        for(int i=0;i<length;i++){
            sum+=validBits.get(i)*Math.pow(10,length-i-1);
        }
        if(sign<0){
            if(sum*sign<(double)Integer.MIN_VALUE){
                return Integer.MIN_VALUE;
            }else{
                return (int)(sum*sign);
            }
        }else{
            if(sum>(double)Integer.MAX_VALUE){
                return Integer.MAX_VALUE;
            }else{
                return (int)sum;
            }
        }

    }

    public boolean isNum(String str, int index){
        int asc = (int)str.charAt(index);
        if(asc>='0' && asc<='9'){
            return true;
        }
        return false;
    }
}
FacebookTwitterGoogle+Share

Leave a Reply

Your email address will not be published. Required fields are marked *