7. Reverse Integer

来源:互联网 发布:常见毒药 知乎 编辑:程序博客网 时间:2024/06/06 16:46

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

public class Solution {    public int reverse(int x) {        if(x == 0)            return 0;        int result = 0;        while(x != 0){            if(result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10)                return 0;            result = result * 10 + x % 10;            x /= 10;        }        return result;    }}
原创粉丝点击