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

Leetcode: Reverse Integer

时间:2015-03-16 20:55:58      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    int reverse(int x) {
        int flag = 1;
        long long x1 = x;
        if(x1 < 0) {
            flag = -1;
            x1 = 0-x1;
        }
        int remaind;
        long long result=0;

        while(x1){
            remaind = x1%10;
            x1 = x1 /10;
            result = result*10 + remaind ;
        }
        if(flag > 0 && result > 2147483647) return 0;
        if(flag<0 && result > 2147483648) return 0;
        return flag < 0? -result:result;
    }
};

  注意溢出问题

Leetcode: Reverse Integer

标签:

原文地址:http://www.cnblogs.com/chdxiaoming/p/4342651.html

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