LeetCode 007 ReverseInteger

7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.
class Solution {
public:
    int reverse(int x) {
    }
};
解题思路:
  • 自己的解题思路
因为要考虑溢出情况,所以经典的转换方法不能直接用,需要做点改变。
10000 00003  倒序后变成30000 00001会出现溢出情况。
我的方法是:用个string str_max来记录MAX_INT,则【-MAX_INT, MAX_INT】可以通过转化为【0, MAX_INT】来解决问题。对于MIN_INT直接处理。
题目明确说了, 溢出返回0
  • 别人的解题思路
通过用long long 来记录int值,不推荐用long. 这样就可以直接用经典倒序算法直接对int x进行倒序。此方法很巧妙,需突破思维禁锢。
同时,他将正数跟负数一起处理,不需要分情况讨论。
学习收获:
  • 学到一招,小范围数转到大范围数,来防止溢出。
  • 对于已知一个string str;求它的倒序,可以string temp(str.rbegin(), str.rend());
还要更简单的方法?可以直接对它进行倒序?
附件:程序
1、自己的程序:
int reverse ( int x )
     {
         if ( INT_MIN == x )
         {
             return 0 ;
         }
         int ux = ( x > 0 ) ? x : - x ;
         int flag = ( x > 0 ) ? 1 : - 1 ;
         int temp = ux ;
         int len = 0 ;
         while ( temp )
         {
             len ++;
             temp /= 10 ;
         }
         if ( len < 10 )
         {
             int res = 0 ;
             while ( ux )
             {
                 res = res * 10 + ux % 10 ;
                 ux /= 10 ;
             }
             return res * flag ;
         }
         else
         {
             string str1 , str ;
             int max_tem = INT_MAX ;
             temp = ux ;
             while ( temp )
             {
                 str1 . push_back ( temp % 10 + '0' );
                 str . push_back ( max_tem % 10 + '0' );
                 temp /= 10 ;
                 max_tem /= 10 ;
             }
             string str_max ( str . rbegin (), str . rend ());
             if ( str1 > str_max )
             {
                 return 0 ;
             }
             else
             {
                 int res = 0 ;
                 while ( ux )
                 {
                     res = res * 10 + ux % 10 ;
                     ux /= 10 ;
                 }
                 return res * flag ;
             }
         }
     }
2、别人的程序
int reverse ( int x )
{
     long long r = 0 ;
     while ( x ) r = r * 10 + x % 10 , x /= 10 ;
     return ( int ( r ) == r ) * r ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值