码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode 413. 反转整数

时间:2018-05-02 13:31:16      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:max   tps   The   get   www   solution   ref   integer   min   

题目:

  • LintCode 413. Reverse Integer
  • 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例:

  • 给定 x = 123,返回 321
  • 给定 x = -123 ,返回 -321

实现:

  • Java实现代码

    public class Solution {
    /**
     * @param n: the integer to be reversed
     * @return: the reversed integer
     */
    public int reverseInteger(int n) {
            // write your code here
            boolean negative = n < 0;
            if(negative) n = -n;
            long r = 0;
            while(n>0){
                r = r*10 + n%10;
                n=n/10;
            }
            if(negative) r = -r;
            if(r>Integer.MAX_VALUE||r<Integer.MIN_VALUE) return 0;
            return (int)r;
        }
    }

LintCode 413. 反转整数

标签:max   tps   The   get   www   solution   ref   integer   min   

原文地址:https://www.cnblogs.com/hglibin/p/8979248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!