判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-number
int gaowei(int x){
while(x>=10){
x/=10;
}
return x;
}
int high(int count){
int i,sum=1;
for(i=1;i<=count;i++){
sum*=10;
}
return sum;
}
int len_num(int tmp){
int count=0;
while(tmp>=10){
count++;
tmp/=10;
}
count++;
return count;
}
bool isPalindrome(int x){
int i,j,tmp,tmp1,count=0;
if(x==0) return true;
if(x<0 || x%10==0) return false;
if(x<10) return true;
tmp=x;
count=len_num(tmp);
// printf("%d\n",count);
tmp=(count+1)/2;
while(tmp>0){
i=x%10;
j=gaowei(x);
if(len_num(x)<count) j=0;
// printf("i=%d,j=%d\n",i,j);
if(i!=j) return false;
x=x/10;
count-=2;
x-=j*high(count);
tmp--;
}
return true;
}
来源:CSDN
作者:2018魔方
链接:https://blog.csdn.net/qq_36481821/article/details/103743803