Leetcode8:atoi()

来源:互联网 发布:d盾webshell 编辑:程序博客网 时间:2024/04/29 20:15

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.


public class Solution {    public int atoi(String str) {        // Start typing your Java solution below        // DO NOT write main() function        int res=0;        boolean started=false;        boolean flag = false;        for(int i=0; i<str.length(); i++){            // trim space chars at the begining and handle '+'/'-'            if(!started){                if(str.charAt(i) == ' ')                    continue;                if(str.charAt(i) == '-'){                    flag = true;                    started = true;                    continue;                }                if(str.charAt(i) == '+'){                    started = true;                    continue;                }            }            started = true;            int digit = str.charAt(i) - '0';                        // meet non-number char at middle            if(digit<0 || digit>9)                break;                        // trim '0' at the head            if(res!=0 || digit!=0){                // positive                if(!flag){                    if(res>Integer.MAX_VALUE/10                        || (res==Integer.MAX_VALUE/10 && digit>Integer.MAX_VALUE%10)){                        res = Integer.MAX_VALUE;                        return res;                    }                    else                        res = res*10+digit;                }                // negtive                else{                    if(res>Integer.MAX_VALUE/10                        || (res==Integer.MAX_VALUE/10 && digit>-(Integer.MIN_VALUE%10))){                        res = Integer.MIN_VALUE;                        return res;                    }                    else                        res = res*10+digit;                }            }        }        if(flag)            res = -res;        return res;    }}

A lot of mistakes have been made.

DO it again, later!!


------------------------------------------------------------------------------------------------------------------------------------------

LL's solution:

public class Solution {    public int atoi(String str) {        // Start typing your Java solution below        // DO NOT write main() function        int len = str.length();        long res = 0;        boolean stop = false;        boolean pos = true;        for(int i = 0; i<len; i++){            char c = str.charAt(i);            if(!stop){                if(c==' '){                    //no digit before the space                    if(i!= 0 && !Character.isDigit(str.charAt(i-1)))                        continue;                    else if(i==0)                        continue;                    else                        break;                }                else if(c=='+'){                    //follow with digit                    if(i!= len-1 && Character.isDigit(str.charAt(i+1)))                        continue;                    else                        break;                }                 else if(c=='-'){                    //follow with digit                    if(i!= len-1 && Character.isDigit(str.charAt(i+1)))                         pos = false;                    else                        break;                                    }                else if(Character.isDigit(c)){                    int digit = Character.getNumericValue(c);                    res = res*10 + digit;                                   }                else                    stop = true;            }            else                break;        }        if(!pos)            res = -res;                    if(res>Integer.MAX_VALUE)            res = Integer.MAX_VALUE;        if(res<Integer.MIN_VALUE)            res = Integer.MIN_VALUE;                    return (int)res;    }}
Consider these special case:

1. space in the beginning, ' 123' -> 123

2. space in the middle, '123 456' -> 123

3. +/-, '+123' -> 123, '++123' -> 0

4. overflow, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned. 

原创粉丝点击