English: leetcode.com/problems/pa…

English: leetcode-cn.com/problems/pa…

Topic describes

Checks whether an integer is a palindrome. Palindromes are integers that read in positive (left to right) and backward (right to left) order.

Example 1:

Input: 121 Output:true
Copy the code

Example 2:

Input: -121 Output:falseInterpretation: Read from left to right as -121. Read from right to left, 121-. So it's not a palindrome number.Copy the code

Example 3:

Input: 10 Output:falseInterpretation: Read from right to left as 01. So it's not a palindrome number.Copy the code

Answer key

/**
 * @param {number} x
 * @return{Boolean} */ / convert an integer to a string // Compare the first and last bits, and compare the second and last bits, and so on // If the number has n bits, compare at most n/2 times. // 123 // 1221 var isPalindrome =function (x) {
    let xStr = x.toString();
    for (let i = 0; i < xStr.length / 2; i++) {
        if(xStr[i] ! = xStr[xStr.length - 1 - i]) {return false; }}return true;
};
Copy the code