美文网首页
007,Reverse Integer

007,Reverse Integer

作者: 丹之 | 来源:发表于2018-10-08 08:33 被阅读0次

    https://leetcode.com/problems/reverse-integer/description/

    Given a 32-bit signed integer, reverse digits of an integer.

    Example 1:

    Input: 123 Output: 321

    Example 2:

    Input: -123 Output: -321

    Example 3:

    Input: 120 Output: 21

    https://leetcode.com/problems/reverse-integer/discuss/4056/Very-Short-(7-lines)-and-Elegant-Solution

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

    相关文章

      网友评论

          本文标题:007,Reverse Integer

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