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.

spoilers alert… click to show requirements for atoi.

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.

This question is purely implementation, you need to think of all the possible inputs.

First trim off all the trailing and heading spaces.
Second record the sign bit.
Then find the valid string (find the first non-digit char from left).
Then do the conversion, we use long to prevent overflow.
Then append the sign bits back and check if the final result is out of range or not.

public class Solution {
    public int atoi(String str) {
        str = str.trim(); //get rid of the heading and trailing spaces, the new string is returned and original string is un touched
        if (str.length() == 0)
            return 0;
        if (str.length() == 1)
            return str.charAt(0) - '0';
        if (!Character.isDigit(str.charAt(0)) && str.charAt(0) != '-' && str.charAt(0) != '+')
            return 0;
        boolean sign = false;
        long power = 1;
        if (str.charAt(0) == '-' || str.charAt(0) == '+') { // remove sign bit
            sign = str.charAt(0) == '-';
            str = str.substring(1);
        for (int i = 0; i < str.length(); i++){
            if (!Character.isDigit(str.charAt(i)))
                str = str.substring(0, i);
        for (int i = 0; i < str.length(); i++) {
        long res = 0;
        for (int i = 0; i = Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        if (res <= Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        return (int)res;

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s