leetcode 细节实现题

leetcode 细节实现题_第1张图片

解法1:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) return false;
        int d = 1;
        while(x / d >= 10) d *= 10;
        while(x){
            int p = x / d;
            int q = x % 10;
            if(p != q)
                return false;
            x = x % d / 10;
            d /= 100;
        }
        return true;
    }
};

解法2:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0)
            return false;
        double r = 0;
        int x1 = x;
        while(x1){
            r = r * 10 + x1 % 10;
            x1 /= 10;
        }
        if(r == x)
            return true;
        else
            return false;
    }
};


你可能感兴趣的:(leetcode 细节实现题)