Palindrome Number

In this post we discuss ways to determine whether an integer is a palindrome.

这里我们讨论求一个整数是否是一个回文数。(回文数,举个例子12344321)从左到右,与右到左是一样的。

Hint:
Don’t be deceived by this problem which seemed easy to solve. Also note the restriction of doing it without extra space. Think of a generic solution that is not language/platform specific. Also, consider cases where your solution might go wrong.

不要被这个看上去简单的问题欺骗了。注意做这个没有额外空格的限制。

Solution:
First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. For the purpose of discussion here, we define negative integers as non-palindromes.

首先,这个问题没有指出是否负整数是一个回文数。比如负整数-1是一个回文数吗? 在这里我们定义负整数不是回文数。

The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate. Although this certainly does work, it violates the restriction of not using extra space. (ie, you have to allocate ncharacters to store the reversed integer as string, where n is the maximum number of digits). I know, this sound like an unreasonable requirement (since it uses so little space), but don’t most interview problems have such requirements?

最直观的方法是表示整数为一个字符串,因为这样跟容易操作。尽管这样肯定能行,他违反了不用额外空格的限制(比如你不得不分配N个字符去保存反向的整数作为字符串,这就好比不合理的要求,因为它使用了很小很小的空格)但是大多数面试没有如此的要求?

Another approach is to first reverse the number. If the number is the same as its reversed, then it must be a palindrome. You could reverse a number by doing the following:

另外一个方法就是先反转这个数字。如果这个数字和他的反转相同,那么他一定是回文数。你可以反转一个数字通过下面的方法:

int reverse ( int num ) {
   assert ( num >= 0 ) ;    // for non-negative integers only.
   int rev = 0 ;
   while ( num != 0 ) {
     rev = rev * 10 + num % 10 ;
     num /= 10 ;
   }
   return rev ;
}
This seemed to work too, but did you consider the possibility that the reversed number might overflow? If it overflows, the behavior is language specific (For Java the number wraps around on overflow, but in C/C++ its behavior is undefined). Yuck.

这个看上去行,但是你考虑这个反转的数字可能溢出嘛?如果溢出,这个动作就是语言特性。比如JAVA,数字

Of course, we could avoid overflow by storing and returning a type that has larger size than int (ie, long long). However, do note that this is language specific, and the larger type might not always be available on all languages.

当然我们可以通过保存避免溢出以及返回一个类型,这个类型有个更大的尺寸比起int比如long。但是,注意到这是一个语言特性,并且更大的类型可能不是总是可得到的在所有语言中。

We could construct a better and more generic solution. One pointer is that, we must start comparing the digits somewhere. And you know there could only be two ways, either expand from the middle or compare from the two ends.

我们能构造更好的更聪明的办法。一点就是,我们必须开始在某处比较数字。以及你知道那里有可能只有2个方向,要么从中延伸,或者从2头比较。

It turns out that comparing from the two ends is easier. First, compare the first and last digit. If they are not the same, it must not be a palindrome. If they are the same, chop off one digit from both ends and continue until you have no digits left, which you conclude that it must be a palindrome.

结果是,从2头比较比较容易。首先,比较第一个和最后一个。如果不一样,肯定不是回文数。如果一样从2头砍掉一个数字然后继续直到没有数字,这样你推出他一定是一个回文数。

Now, getting and chopping the last digit is easy. However, getting and chopping the first digit in a generic way requires some thought. I will leave this to you as an exercise. Please think your solution out before you peek on the solution below.

现在获得和砍掉最后一位很简单。但是获得和砍掉第一位用聪明的方法需要一些想法。
bool isPalindrome ( int x ) {
   if ( x < 0 ) return false ;
   int div = 1 ;
   while ( x / div >= 10 ) {
     div * = 10 ;
   }         
   while ( x != 0 ) {
     int l = x / div ;
     int r = x % 10 ;
     if ( l != r ) return false ;
     x = ( x % div ) / 10 ;
     div /= 100 ;
   }
   return true ;
}


这个就是利用整数利用除法和余数获得第一位和最后位数字,然后比较。只能说太机智了。

Alternative Solution:
Credits go to Dev who suggested a recursive solution (if extra stack space does not count as extra space), which is pretty neat too.

这个看的有点晕。下次再仔细看,先发表了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值