Determine whether an integer is a palindrome. Do this without extra space.
%3 分解出所有元素后放入vector,每个值中间插入一个1,这样所有的都会变成奇数个。然后从中间开始 循环判断是否符合要求
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
int iLen = 0;
int iCopy = x;
vector<int> vInt;
while (iCopy != 0)
{
vInt.push_back(1);
vInt.push_back(iCopy%10);
iCopy = iCopy / 10;
}
vInt.push_back(1);
iLen = vInt.size();
int iBegin = (iLen - 1) / 2;
for (int i = 0;i <= iBegin;i++ )
{
if (vInt[iBegin - i] != vInt[iBegin+i])
{
return false;
}
}
return true;
}
};
这种就是直接判断。65%
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
int len = 1;
for (len = 1; (x / len) >= 10; len *= 10);
while (x != 0)
{
int left = x / len;
int right = x % 10;
if (left != right)
{
return false;
}
x = (x%len) / 10;
len /= 100;
}
return true;
}
};