Simple Java Soluion


#1

public int atoi(final String A) {

    int a=0;
    int sign = 1;
    
    for(int i=0; i<A.length(); i++){
        if (A.charAt(i) == '-')
            sign = -1;
    
        Character ch = new Character(A.charAt(i));
        if(Character.isDigit(ch))
            a = a*10 + A.charAt(i) -'0';
        else if(A.charAt(i)==' ' || Character.isLetter(ch))
            break;
        else
            continue;
            
         if(i<A.length()-1 && A.charAt(i+1)!=' ' ) {
            if (a > Integer.MAX_VALUE / 10
                    || (a == Integer.MAX_VALUE / 10
                        && A.charAt(i) - '0' > 7))
                {
            	 
                    if (sign == 1)
                        return Integer.MAX_VALUE;
                    else
                        return Integer.MIN_VALUE;
                }          
            }
    }
    
   
    return sign*a;

}