LeetCode # 9 simple questions (judgment digit palindrome)

Title: Analyzing integer is not a palindrome

Solution: For negative numbers or numbers ending in 0 and not to zero, it is certainly false, for the other number, use another number y after the deposit with numbers anyway, and then began to dump the anti-x y, where y> = x when the reverse stop, after all x and y and y, if x and y are equal (even numbers) or y / 10 is equal to x (odd number), then it is a palindromic

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0 || (x % 10 == 0 && x != 0)) return false;
        int left = x, right = 0;
        while (left > right){
            right = right * 10 + left %10;
            left = left / 10;
        }
        if (left == right || left == right / 10){
            return true;
        }
        else{
            return false;
        }
    }
};

 

Guess you like

Origin www.cnblogs.com/error408/p/11610611.html