LeetCode算法题之第8题String to Integer (atoi)

Question:

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.

解决:

将字符串变为数字。需要注意坑很多。

public int myAtoi(String str) {
    int sum = 0;
    int flag = 1;
    str = str.trim();
    int i = 0;
    if (str.length() == 0)
        return sum;
    if (str.charAt(0) == '-' || str.charAt(0) == '+'){
        if (str.charAt(0) == '-')
            flag = -1;
        ++i;
    }
    while(i < str.length() && Character.isDigit(str.charAt(i))){
        if (judge(flag, sum, str.charAt(i)-'0')){
            if (flag == 1)
                sum = Integer.MAX_VALUE;
            else
                sum = Integer.MIN_VALUE;
            break; 
        }
        sum = sum * 10 + (str.charAt(i) - '0');
        ++i;
    }
    return flag == 1 ? sum : -sum;
}
public boolean judge(int flag, int sum, int digit){
    boolean result = false;
    if (flag == 1 && (sum > Integer.MAX_VALUE/10 || sum == Integer.MAX_VALUE/10 && digit > Integer.MAX_VALUE%10))
        result = true;
    if (flag == -1 && (-sum < Integer.MIN_VALUE/10 || -sum == Integer.MIN_VALUE/10 && -digit < Integer.MIN_VALUE%10))
        result = true;
    return result;
}

代码地址(附测试代码):
https://github.com/shichaohao/LeetCodeUsingJava/tree/master/src/stringToInteger

你可能感兴趣的:(LeetCode算法题之第8题String to Integer (atoi))