Lets keep it Simple Java solution O(|A|)


#1
public class Solution {
    // DO NOT MODIFY THE LIST. IT IS READ ONLY
    public int atoi(final String a) {
        String s = a.trim();
        int l = s.length(), i = 0;
        boolean negative = s.charAt(0)=='-';
        long res = 0;
        if(negative || s.charAt(0)=='+')
            i = 1;
        while(i < l) {
            if(Character.isDigit(s.charAt(i)))
                res=res*10+Integer.parseInt(Character.toString(s.charAt(i++)));
            else
                return negative?(int)(-1*res):(int)res;
            if(negative && (res*-1)<=Integer.MIN_VALUE)
                return Integer.MIN_VALUE;
            else if(res>=Integer.MAX_VALUE)
                return Integer.MAX_VALUE;
        }
        return negative?(int)(-1*res):(int)res;
    }
}

#2

why did u use s.charAt(0)==’+’ does it helps us to identify positive integer


#3

charAt(0)==’+’ is used for positive numbers like ‘-’ can be in front of every negative number similarly we can put ‘+’ for positive numbers it is just optional… So it is not necessary but you can do it and it must be considered a valid input… that’s it…