Saturday, February 20, 2016

[leetcode] 8. 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.
1.  Very boring problem. Not sure possible inputs before coding.
https://github.com/csehao/LeetCode/blob/master/Java/StringtoInteger.java

public class Solution {
    public int myAtoi(String str) {
        int len = str.length();
        int res = 0;
        boolean pn = true;
        boolean hasValue = false;
        boolean pnvalid = false;
        
        int md10 = Integer.MAX_VALUE/10;
        int md10tail = Integer.MAX_VALUE - md10;
        // It is necessary to Integer.MIN_VALUE as well. Omitted here.

        int nmd10 = Integer.MAX_VALUE/10;
        
        for(int i = 0; i < len; ++i){
            Character c = str.charAt(i);
            if('+' == c){
                if(pnvalid == true)
                    return 0;
                pn = true;
                pnvalid = true;
            }else if('-' == c){
                if(pnvalid == true)
                    return 0;
                pn = false;
                pnvalid = true;
            }else if(Character.isDigit(c)){
                
                if(res > md10){
                    return pn?Integer.MAX_VALUE:Integer.MIN_VALUE;    
                }
                res*= 10;
                int value = (int)(Character.getNumericValue(c));
                if(Integer.MAX_VALUE - value < res)
                    return pn?Integer.MAX_VALUE:Integer.MIN_VALUE;
                res+= value;
                hasValue = true;
            }else if(c == ' '){
                if(false == hasValue && pnvalid == false)
                    continue;
                    
                if(hasValue || pnvalid == true){
                    return pn?res:-1*res; 
                }
                
            }else{
                if(hasValue){
                    return pn?res:-1*res;
                }
                else{
                    return 0;
                }
            }
        }
        
        return pn?res:-1*res;
        
    }
}


It is necessary to test the mod behavior under positive and negative values.


No comments:

Post a Comment