【LeetCode】T9 回文数(简单)

LeetCode 9.回文数

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

    int getNum(int x,int num){
        x = x % (int)pow(10,num);
        x = x / pow(10,num-1);
        return x;
    }
    bool isPalindrome(int x) {
        //特殊情况判断
        if(x<0)
            return false;
        if(x>=0 && x<=9)
            return true;
        //进入正题
        int i=x,length=0;
        while(i!=0){
            i /= 10;
            length++;
        }//count length
        i=1;
        while(i<length){
            if(getNum(x,i)!=getNum(x,length)) //出现不同
                return false;
            else{
                i++;length--;
            }
        }
        return true;
    }

 

猜你喜欢

转载自blog.csdn.net/PPPPluie/article/details/88803252
t9
今日推荐