LeetCode_9 回文数

◇◆丶佛笑我妖孽 提交于 2019-12-28 16:39:20

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

示例 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;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!