class Solution {
public:
char temp[1000];//暫存拆解出來的數字
string str1;
string str2;
bool isPalindrome(int x) {
if (x<0) {
return false;
}
if (x>0) {
//數位拆解
int cnt=0;
while (x) {
temp[cnt++]=x%10+'0';
x/=10;
}
str1=(string)temp;
str2=str1;
reverse(str1.begin(), str1.end());
if (str1==str2) {
return true;
}else
return false;
}
return true;//x==0
}
};
做法2:
數字逆置:
class Solution {
public:
bool isPalindrome(int x){
int original=x;
if (x<0) {
return false;
}
if (x>0) {
//reverse integer
int tmp=0;
while (x) {
tmp=tmp*10+x%10;
x/=10;
}
if (original==tmp) {
return true;
}else
return false;
}
return true;//x==0
}
};