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

Leetcode-7 Reverse Integer

时间:2016-12-16 07:43:57      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:example   min   tco   题解   integer   cpp   nbsp   max   highlight   

#7. Reverse Integer          

Reverse digits of an integer.

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

题解:暴力做,注意边界:

class Solution {
public:
    int reverse(int x) {
        long long num=abs(x);
        long long ret=0;
        int tmp=0;
        while(num>9)
        {
            tmp=num%10;
            ret=ret*10+tmp;
            num/=10;
        }
        ret=ret*10+num;
        
        if(ret>INT_MAX||x==INT_MIN)
        return 0;
        else if(x<0)
        return -ret;
        else
        return ret;
    }
};

  

Leetcode-7 Reverse Integer

标签:example   min   tco   题解   integer   cpp   nbsp   max   highlight   

原文地址:http://www.cnblogs.com/fengxw/p/6185479.html

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