LeetCode---7. Reverse Integer

来源:互联网 发布:json格式怎么打开 mock 编辑:程序博客网 时间:2024/06/04 19:19

Reverse digits of an integer.

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

click to show spoilers.

Subscribe to see which companies asked this question

//翻转过来可能会溢出

Input:1534236469
Output:1056389759
Expected:0
public class Solution {    public int reverse(int x) {        long tmp = x;        // 防止结果溢出        long result = 0;        while (tmp != 0) {            result = result * 10 + tmp % 10;            tmp = tmp / 10;        }        // 溢出判断         if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {            result = 0;        }        return (int) result;    }}


0 0