标签:
Determine whether an integer is a palindrome. Do this without extra space.
判断一个整数是否是回文数,
class Solution {
public:
bool isPalindrome(int x) {
bool result=true;
if (x==0)
{
return result;
}
if (x<0)
{
return false;
}
int temp;
vector<int >array;
while(x/10!=0)
{
temp=x%10;
x=x/10;
array.push_back(temp);
}
if (x%10!=0)
{
array.push_back(x%10);
}
for (vector<int>::iterator iter=array.begin(),iter1=array.end()-1;iter!=array.end(),iter1!=array.begin();iter++,iter1--)
{
if (*iter!=*iter1)
{
result=false;
break;
}
}
return result;
}
};优点:方法直观通用class Solution {
public:
bool isPalindrome(int x) {
if(x<0||(x!=0&&x%10==0)) return false;
int sum=0;
int temp=x;
while(temp)
{
sum=sum*10+temp%10;
temp/=10;
}
return (x==sum);
}
};版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/47378761