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

Reverse digits of an integer.

时间:2016-03-17 02:00:45      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    int reverse(int x) {
       long ans=0;//long 是怕中间过程溢出
        int min=1<<31,max=-min-1;//判断溢出,<<为左移运算while(x!=0){
            ans=ans*10+x%10;
            x=x/10;
        }
        if(ans>max||ans<min)
            return 0;
        return ans;
    }
};

 注释:

  这是进制转换问题。

  转化过程中一定要考虑溢出。

  清楚各种类型的表示范围。

附 

32位平台:

unsigned   int   0~4294967295  
int    -2147483648~2147483647 (-2^31~(2^31-1))//补码比原码多表示1个数
unsigned long 0~4294967295
long   -2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

 

Reverse digits of an integer.

标签:

原文地址:http://www.cnblogs.com/wqkant/p/5285795.html

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