Palindrome Number
Palindrome Number
问题:
Determine whether an integer is a palindrome. Do this without extra space.
思路:
常用的进制遍历方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
while(num != 0) { remian = num % 进制; num /= 进制; }
我的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Solution { public boolean isPalindrome(int x) { if(x < 0) return false; int reverse = reverse(x); return reverse == x; } public int reverse(int x) { int rst = 0; while(x != 0) { rst = rst * 10 + x % 10; x /= 10; } return rst; } }
他人代码:
class Solution { public: bool isPalindrome(int x) { //negative number if(x < 0) return false; int len = 1; while(x / len >= 10) len *= 10; while(x > 0) { //get the head and tail number int left = x / len; int right = x % 10; if(left != right) return false; else { //remove the head and tail number x = (x % len) / 10; len /= 100; } }
学习之处:
- 常见数字访问方法,while(num != 0) num /= 进制数
- 常用的进制增加方法, num = num * 进制 + some remain
posted on 2015-03-11 10:57 zhouzhou0615 阅读(127) 评论(0) 编辑 收藏 举报