标签:
Palindrome Number:Determine whether an integer is a palindrome. Do this without extra space.
题意:判断一个整数是否是回文数,且不可以使用额外的空间。
思路:首先负数不是回文数,然后每次取出数的最高位和最低位,进行判断。
代码:
public class Solution { public boolean isPalindrome(int x) { if(x<0) return false; int div = 1; while(x/div >=10){ div *= 10; } while(x!=0){ int left = x/div; //求出最高位 int right = x%10; //求出最低位 if(left!=right) return false; //进行判断 x = (x%div) / 10; // 更新x div/=100; } return true; } }
标签:
原文地址:http://www.cnblogs.com/Lewisr/p/5245566.html