标签:style blog http color io os ar div sp
Determine whether an integer is a palindrome. Do this without extra space.
推断一个数整数是不是回文?比如121,1221就是回文,好吧,直接利用前面写过的【Leet Code】Reverse Integer——“%”你真的懂吗?
只是这里要考虑翻转后,数值溢出的问题,代码例如以下:
/*
//first method
class Solution {
public:
bool isPalindrome(int x)
{
long long temp = x;
long long ret = 0;
bool isNegative = false;
if (temp < 0)
{
return false;
}
while (temp)
{
ret = ret * 10 + temp % 10;
temp /= 10;
}
if(x == ret)
{
return true;
}
else
{
return false;
}
}
};
*/class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0)
{
return false;
}
int divisor = 1;
while (x / divisor >= 10)
{
divisor *= 10;
}
while (x)
{
if (x / divisor != x % 10)
{
return false;
}
x = (x % divisor) / 10;
divisor /= 100;
}
return true;
}
};
标签:style blog http color io os ar div sp
原文地址:http://www.cnblogs.com/gcczhongduan/p/3992590.html