Leetcode–String to Integer (atoi)

The Problem:

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.


  1. The key to solve a problem like this is :
    1. clarify the problem, consider how the function would behave for all corner cases. 2. pay attention to details.
  2. One thing to mention for all the conversion to integer: be careful about the range.
    1. Fact: INT_MAX (2147483647) !=  – INT_MIN (-2147483648)

Java Solution

public class Solution {
    public int atoi(String str) {
        int result = 0;
        str =str.trim();
            return result;
        boolean neg = false;
        boolean nxtN = false;
        for(int i = 0; i < str.length(); i++){
            if(result==0 && str.charAt(i)=='-'){
                if(nxtN)return 0;
                neg = true;
                nxtN = true;
            else if(result ==0 && str.charAt(i)=='+' ){
                if(nxtN)return 0;
                neg = false;
                nxtN = true;
            //breaks a valid number
            else if(!(str.charAt(i)>='0' && str.charAt(i)<='9')){
            else if(str.charAt(i)>='0' && str.charAt(i)<='9'){
                nxtN = true;
                int temp = str.charAt(i)-'0';
                // check the integer bound
                // note the condition for temp is > not >=
                // watch out that Integer.MIN_VALUE != -Integer.MAX_VALUE 
                if((result  == (Integer.MAX_VALUE)/10 && temp > (Integer.MAX_VALUE%10))||
                (result > (Integer.MAX_VALUE)/10)){
                        return neg ? Integer.MIN_VALUE: Integer.MAX_VALUE;
                    result = result *10 + temp;
        return neg ? 0-result : result;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s