Leetcode - Reverse Integer

Question:
Reverse digits of an integer.
Example1: x = 123, return 321Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer’s last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

My code:

public class Solution {
    public int reverse(int x) {
        boolean isPositive = true;
        if (x == 0)
            return 0;
        else if (x == Integer.MIN_VALUE)
            return 0;
        else if (x < 0)
            isPositive = false;
        int temp = Math.abs(x);
        String tempStr = Integer.toString(temp);
        String reverseStr = reverseStr(tempStr);
        if (!isPositive)
            reverseStr = '-' + reverseStr;
        if (Long.parseLong(reverseStr) < -2147483648 || Long.parseLong(reverseStr) > 2147483647)
            return 0;
        return Integer.parseInt(reverseStr);
    }
    
    private String reverseStr(String tempStr) {
        boolean isRightZero = true;
        String reverseStr = "";
        for (int i = tempStr.length() - 1; i >= 0; i--) {
            if (tempStr.charAt(i) != '0' && isRightZero)
                isRightZero = false;
            if (!isRightZero)
                reverseStr += tempStr.charAt(i);
        }
        return reverseStr;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        System.out.println(test.reverse(-2147483648));
    }
}

My test result:

《Leetcode - Reverse Integer》 Paste_Image.png

这次作业比较简单,其实就是反转字符串。但是要先判断下正负情况。
**
总结:
然后仔细研究了下 Math.abs(int a) 这个方法。他有一个特殊情况。
当 a = Integer.Min_VAL 时,此时如果返回绝对值,会造成溢出。所以这个方法规定仍旧返回原值。所以需要提前将这个情况用if语句考虑到。然后就基本没什么问题了。
**
Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int reverse(int x) {
        int sign = (x >= 0 ? 1 : -1);
        long num = Math.abs((long) x);
        long temp = 0;
        while (num > 0) {
            long digit = num % 10;
            temp = 10 * temp + digit;
            num = num / 10;
        }
        
        if (sign == 1) {
            if (temp > Integer.MAX_VALUE) {
                return 0;
            }
            else {
                return (int) temp;
            }
        }
        else {
            if (-temp < Integer.MIN_VALUE) {
                return 0;
            }
            else {
                return (int) -temp;
            }
        }
    }
}

感觉和 实现两个数相除,还有两个数相除算循环小数的题目,差不多。
就是进来先判断sign,然后转换成long,再做后面的事。

Anyway, Good luck, Richardo! — 09/23/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/cdca76294e46#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞