首页 > 其他 > 详细

[LeetCode] No. 9 Palindrome Number

时间:2016-08-26 13:44:54      阅读:136      评论:0      收藏:0      [点我收藏+]

[题目] Determine whether an integer is a palindrome. Do this without extra space.

[题目解析] 判断一个给定整数是否为回文数,回文数即121,11411这种正着和反着相同的数字,最小的回文数是0。实现思路可以比较直接,先对int进行reverse,这个可以参考

http://www.cnblogs.com/zzchit/p/5806956.html,然后和给定数字比较即可。但是这道题困难的在这里“Do this without extra space.”,这就需要另想它法。然而并没有想到不需要额外空间的方法,

所以只能把题目规定的"额外空间"不包括这种O(1)的空间。如有好的方法,再做讨论。根据直接的思路,可以对reverse integer的代码进行化简,如下。

1   public boolean isPalindrome(int x) {
2         if(x < 0 || (x>0 && x%10==0)) return false;
3         int result = 0;
4         while(x > result){
5             result = result*10+x%10;
6             x/=10;
7         }
8         return (x == result || x == result/10);
9     }

 

[LeetCode] No. 9 Palindrome Number

原文:http://www.cnblogs.com/zzchit/p/5809859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!