“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

The title

Given an integer x, return true if x is a palindrome integer; Otherwise, return false.

Palindromes are integers that read in positive (left to right) and backward (right to left) order. For example, 121 is palindrome and 123 is not.

Example 1:

Enter: x =121Output:true
Copy the code

Example 2:

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

Example 3:

Enter: x =10Output:falseRead from right to left, as01. So it's not a palindrome number.Copy the code

Example 4:

Input: x = -101 Output: falseCopy the code

Tip:

  • -231 <= x <= 231 - 1

Their thinking

Reverse the number itself and compare the reversed value with the original value (but there is a problem, that is, if the reversed number is too large, there will be integer overflow problem, so far we have not thought of a good solution, and we will update the article at any time in the future)

Specific analysis:

1. Convert the number to a string number.toString()

2. Then split the string into the array string.split()

3. Then switch the data in the array

4. Then consolidate the inverted array into a string

5. Finally, compare the string with the original split string, return true if the string is identical, false if the string is different

Code implementation

/* * @lc app=leetcode.cn id=9 lang=javascript * * [9] Palindrome number */

// @lc code=start
/ * * *@param {number} x
 * @return {boolean}* /
 var isPalindrome = function(x) {
        var xString = x.toString();
        var xStringArr = xString.split(' ');
        var resultStr = ' ';
        for (var i = xStringArr.length-1; i >= 0; i--) {
            resultStr += xString.charAt(i);
        }
    if(resultStr === xString){
        return true;
    } else {
        return false; }}// @lc code=end


Copy the code

Other methods

  • Take the second half of the number out by mod and reverse it to compare with the first half of the number
  • String inversion does not use string thinking = “ten hundred conversion add ok
  • Use the reverse method of js directly