smilevchy's blog

Life & Study & Chasing

Leetcode_Palindrome Number

原题链接: https://oj.leetcode.com/problems/palindrome-number/

public boolean isPalindrome(int x) {
    if (x < 0) return false;

    // get length of digit
    int carryLength = 1;
    while (x / carryLength >= 10) {
        carryLength *= 10;
    }

    while (x > 0) {
        // test if the first and the end digit is equal; if not, return false
        if (x / carryLength != x % 10) return false;

        x = x % carryLength / 10;    // get the new number without the first and the end digit
        carryLength /= 100;    // for cutting the first and the end digit, we need to shrink carry length by 2 times
    }

    return true;
}

algorithm

« Leetcode_Valid Number Leetcode_Count and Say »