首页 > 代码库 > Palindrome Number

Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

思路:想办法把第一个和最后一个数字剥离出来比较

#include<stdio.h>
int isPalindrome(int x){
    int d=x/10,i=1;
    int begin=0,end=0;
    if(x<0) return 0;
    if(x/10==0) return 1;
    while(d!=0){
        d=d/10;
        i=i*10;
    }
    d=x;
    while(i!=0){
        begin=d/i;
        end=d%10;
        //printf("%d,%d,%d,%d\n",begin,end,d,i);
        if(begin!=end) return 0;
        d=(d%i)/10;
        i=i/100;

    }
    return 1;
}

void main(){
    printf("%d\n",isPalindrome(5225));
}


Palindrome Number