Leetcode - String to Integer (atoi)

Question:

Leetcode - String to Integer (atoi)_第1张图片

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:

Leetcode - String to Integer (atoi)_第2张图片
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

你可能感兴趣的:(Leetcode - String to Integer (atoi))