9.Palindrome Number
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。class Solution {
public:
bool isPalindrome(int x) {
if(x<0 || x==10) return false;
if(x<10) return true;
int s=log10(x);
while(s>0){
int f=x/pow(10,s);
int l=x%10;
//cout<< f<<","<<l<<endl;
if (f!=l) return false;
x=(x-f*pow(10,s))/10;
s-=2;
}
return true;
}
};Last updated