Leetcode - String to Integer (atoi)

weixin_34007886發表於2015-07-06

Question:

161212-6abebd71efa7d80a.png

My code:

public class Solution {
    public int myAtoi(String str) {
        if (str == null || str.length() == 0)
            return 0;
        boolean isZero = true;
        boolean isPositive = true;
        
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != ' ') {
                str = str.substring(i, str.length());
                break;
            }
        }
                
        
        
        
        if (str.charAt(0) == '-') {
            isPositive = false;
            str = str.substring(1, str.length());
        }
        else if (str.charAt(0) == '+')
            str = str.substring(1, str.length());
        
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                if (str.charAt(i) == '0' && isZero)
                    continue;
                else {
                    isZero = false;
                    result += str.charAt(i);
                }
            }
            else
                break;;
        }
        if (result.length() == 0)
            return 0;
        if (result.length() > 10) {
            if (isPositive)
                return Integer.MAX_VALUE;
            else
                return Integer.MIN_VALUE;
        }
        long a = Long.parseLong(result);
        if (!isPositive)
            a = (-1) * a;
        if (a < Integer.MIN_VALUE)
            return Integer.MIN_VALUE;
        else if (a > Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
            
        else
            return (int)a;
    }
}

My test result:

161212-68f7c3f90bb98164.png
Paste_Image.png

這道題目沒有什麼意義,就是有一些極端情況,可能沒考慮到。測試的時候哪裡有錯,就把那些有錯的情況考慮到就好了。

**
總結:沒啥意義。
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int myAtoi(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        
        int start = 0;
        int base = 0;
        int sign = 1;
        int i = 0;
        String s = str;
        for (; i < str.length(); i++) {
            if (s.charAt(i) != ' ') {
                break;
            }
        }
        if (s.charAt(i) == '+' || s.charAt(i) == '-') {
            if (s.charAt(i) == '-') {
                sign = -1;
            }
            i++;
        }
        
        while (i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
            char curr = s.charAt(i);
            if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && curr - '0' > 7)) {
                if (sign == 1) {
                    return Integer.MAX_VALUE;
                }
                else {
                    return Integer.MIN_VALUE;
                }
            }
            base = 10 * base + (curr - '0');
            i++;
        }
        return sign * base;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        int ret = test.myAtoi("1");
        System.out.println(ret);
    }
}

reference:
https://discuss.leetcode.com/topic/2666/my-simple-solution

這種題目沒有意思,看下答案就行了。然後記住它。

Anyway, Good luck, Richardo! -- 09/20/2016

相關文章