leetcode 9: Palindrome Number

Reverse the number and compare it with the original one.

class Solution {
public:
    bool isPalindrome(int x) {
        int old_x=x;
        int new_x=0;
        while(x>0)
        {
            if((INT_MAX-x%10)/10<new_x)
                return false;
            new_x=new_x*10+x%10;
            x/=10;
        }
        return new_x==old_x;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章