Palindrome Number

LeetCode #9


Description:

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

Example:

Note

Idea:

看code。

Code:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;

        // 先找到最接近num的
        int denom=1;
        while(x / denom >= 10){
            denom *= 10;
        }

        while(x != 0){
            if(x/denom != x%10) return false;
            x = x%denom /10; // 去头去尾
            denom /= 100; // 首尾都要去,所以除100
        }

        return true;
    }
};

results matching ""

    No results matching ""