您的位置:首页 > 编程语言 > Go语言

leetcode: (9) Palindrome Number

2015-08-29 22:54 302 查看
Determine whether an integer is a palindrome. Do this without extra space.

方法一:首先求出x一共有多少位,然后从比最低位与最高位的值开始比较

低位的值可以通过 x(pow(10,i))%10的得到

高位的值首先要把第i位前面的去掉x- x/pow(10,count-i)*pow(10,count-i)

然后高位的值为(x-
x/pow(10,count-i)*pow(10,count-i))/pow(10,count-i-1)

bool isPalindrome(int x) {
if(x<0) return false;
if(x/10==0) return 1;
int temp=x;
int count=0;
while(temp!=0)
{
count++;
temp/=10;
}
for(int i=0;i<count/2;)
{
temp=(x-(x/(int)pow(10.0,count-i))*(int)pow(10.0,count-i))/pow(10.0,count-1-i);
if(temp==x/(int)pow(10.0,i)%10) i++;
else return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithms leetcode