# LeetCode 8：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.

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.

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */public class Solution {    public int myAtoi(String str) {                if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {            return 0;        }        str = str.trim();                long temp = 0;             int pos = 0;               int flag = 0;                    if (str.charAt(0) == '-') {            flag = -1;            pos ++;        } else if(str.charAt(0) == '+') {            flag = 1;            pos ++;        }                for (; pos < str.length(); pos++) {            if (Character.isDigit(str.charAt(pos))) {                temp = temp * 10 + (str.charAt(pos) - '0');            }        }                if (flag == -1) {            temp = -temp;        }                if (temp > Integer.MAX_VALUE) {            return Integer.MAX_VALUE;        } else if (temp < Integer.MIN_VALUE) {            return Integer.MIN_VALUE;        }        return (int)temp;    }}`

Input:“+-2”
Output:2
Expected:0

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */public class Solution {    public int myAtoi(String str) {                if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {            return 0;        }        str = str.trim();                long temp = 0;             int pos = 0;               int flag = 0;                    if (str.charAt(0) == '-') {            flag = -1;            pos ++;        } else if(str.charAt(0) == '+') {            flag = 1;            pos ++;        }                for (; pos < str.length(); pos++) {            if (Character.isDigit(str.charAt(pos))) {                temp = temp * 10 + (str.charAt(pos) - '0');            } else {                break;            }        }                if (flag == -1) {            temp = -temp;        }                if (temp > Integer.MAX_VALUE) {            return Integer.MAX_VALUE;        } else if (temp < Integer.MIN_VALUE) {            return Integer.MIN_VALUE;        }        return (int)temp;    }}`

Input:“9223372036854775809”
Output:-2147483648
Expected:2147483647

`package com.maoxiaomeng;/** * @author lihui * @date 2018/3/26 12:31 */public class Solution {    public int myAtoi(String str) {                if (str == null || str.length() < 1 || str.equals("+") || str.equals("-")) {            return 0;        }        str = str.trim();                double temp = 0;             int pos = 0;               int flag = 0;                    if (str.charAt(0) == '-') {            flag = -1;            pos ++;        } else if(str.charAt(0) == '+') {            flag = 1;            pos ++;        }                for (; pos < str.length(); pos++) {            if (Character.isDigit(str.charAt(pos))) {                temp = temp * 10 + (str.charAt(pos) - '0');            } else {                break;            }        }                if (flag == -1) {            temp = -temp;        }                if (temp > Integer.MAX_VALUE) {            return Integer.MAX_VALUE;        } else if (temp < Integer.MIN_VALUE) {            return Integer.MIN_VALUE;        }        return (int)temp;    }}`

`System.out.println("Long_MAX:   " + Long.MAX_VALUE);System.out.println("Long_MIN:   " + Long.MIN_VALUE);System.out.println("Double_MAX: " + Double.MAX_VALUE);System.out.println("Double_MIN: " + Double.MIN_VALUE);Long_MAX:   9223372036854775807Long_MIN:   -9223372036854775808Double_MAX: 1.7976931348623157E308Double_MIN: 4.9E-324`

OVER