Nuggets team number online, help you Offer rimmon! Click to see details

I. Topic Description:

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

Palindromes are integers that are read in the same order (from left to right) and in the same order (from right to left). For example, 121 is a palindrome, while 123 is not.

 

Copy the code

Ii. Thinking analysis:

  • Integer to string -> array -> Array reversal -> string, and then compare for equality
  • Refer to integer inversion

Three, AC code

/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
  return String(x) === String(x).split('').reverse().join('');
};
Copy the code

Four,

  • There’s not just one way to do it, there’s a lot of ways to do it,

  • This can be achieved using integer inversion logic

/** * @param {number} x * @return {boolean} */ var isPalindrome = function(x) { if(x < 0 || (! (x % 10) && x)) return false; const num = x; let result = 0; while(x ! == 0) { result = result * 10 + x % 10; x = (x / 10) | 0; } return (result | 0) === result ? result === num: 0; };Copy the code

Of course, I have seen other methods, which may be a little tedious, such as:

1, first calculate the total number of digits 2, each time to compare the first and the end of 3, remove the first and the end of the comparisonCopy the code

For reference only

Refer to the topic

  • Force button (LeetCode)