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

LeetCode--Reverse Integer

时间:2015-07-04 18:24:44      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Reverse digits of an integer.

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

问题描述: 将整数个十百位反序输出。

注意特殊情况:

1)溢出情况:To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why?

2)正数、负数除10 余10的情况;

3)10,100这样的数反序时可不可以用一般的程序处理。

代码:

public class Solution {
    public int reverse(int x) {
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        long res = 0;
        while(x!=0){
            int n = x%10;
            res = res *10 + n;
            x = x/10;
            if(res>max || res<min)
                return 0;
        }
        int res1 = (int) res;
        return res1;
    }
}

 

LeetCode--Reverse Integer

标签:

原文地址:http://www.cnblogs.com/little-YTMM/p/4621021.html

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