美文网首页
Leetcode-7题:Reverse Integer

Leetcode-7题:Reverse Integer

作者: 八刀一闪 | 来源:发表于2016-09-24 16:50 被阅读22次

题目

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

代码:

********需注意整数溢出********

public int reverse(int x) {
    long sum = 0;
    while (x != 0) {
        sum = sum*10 + x%10;
        if (sum>Integer.MAX_VALUE || sum<Integer.MIN_VALUE)
            return 0;
        x /= 10;
    }
    return (int)sum;
}

相关文章

网友评论

      本文标题:Leetcode-7题:Reverse Integer

      本文链接:https://www.haomeiwen.com/subject/jamxyttx.html