07_Reverse Integer

1.Question

2.Solution

public class Solution {
    public int reverse(int x) {
        if (x == 0) return 0;
        int res = 0;
        int sign = 1;
        if (x < 0) {
            sign = -1;
            x = -1 * x;
        }
        while (x != 0) {
            if (res > (Integer.MAX_VALUE - x % 10) / 10) {
                return 0;
            }
            res = res * 10 + x % 10;
            x = x / 10;
        }
        return res * sign;

    }
}

 

3.Test

posted @ 2016-07-01 17:20  桃源仙居  阅读(75)  评论(0)    收藏  举报