“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

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:

Input: x = 121 Output: true

Example 2:

Input: x = -121 Output: false Description: Read from left to right. The value is -121. Read from right to left, 121-. So it’s not a palindrome number.

Example 3:

Input: x = 10 Output: false Description: Read from right to left, 01. So it’s not a palindrome number.

Example 4:

Input: x = -101 Output: false

Note: -231 <= x <= 231-1

Advanced: Can you solve this problem by not converting integers to strings?

Their thinking

From the previous topic 7. Integer inversion we can know that there are two ways to reverse integers:

  • Converts an integer to a string and then to an array for inversion
  • It can be obtained by mathematical method

If we compare whether the number obtained after the inversion is equal to the number before, we can judge whether it is a palindrome number.

If equal, return true; Otherwise return false

Meanwhile, according to the example, we can get the following features:

  • A number less than 0 cannot be a palindrome
  • Anything greater than 0 and less than 10 must be a palindrome
  • An even number of digits cannot be a palindrome

Method 1: Integer inversion – array

/ * * *@param {number} x
 * @return {boolean}* /
var isPalindrome = function(x) {
    return parseInt(x.toString().split(' ').reverse().join(' ')) === x ? true : false;
};
Copy the code

Method two: integer inversion – mod

So this way you have to take into account the positive and negative of x.

And because it changes the value of the x that comes in, we need to save it at the beginning.

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

See the big guy better solution is as follows:

var isPalindrome = function(x) {
    if(x < 0| | (! (x %10) && x)) return false;
    let x2 = x, res = 0;
    while(x2){
        res = res * 10 + x2 % 10;
        x2 = ~~(x2 / 10);
    }
    return res === x;
};
Copy the code

Method three: turn the string and loop the comparison

/ * * *@param {number} x
 * @return {boolean}* /
var isPalindrome = function(x) {
    if (x >= 0) {
        str = x.toString();
        for (let i = 0; i < str.length / 2; i++) {
            if(str[i] ! == str[str.length -1 - i]) return false;
        }
        return true;
    } else {
        return false; }};Copy the code

Method 4: Double pointer after converting to string

  • Now convert the number to a string
  • Create two Pointers, one pointing to left and one pointing to right
  • If left < right, return false if left < right.
  • Otherwise, the left pointer moves one to the right
  • The right hand moves one to the left

Return false if unequal.

Why don’t we just say equal?

We need to take into account that x = 0, we can’t actually go inside the loop, so we should return true outside.

/ * * *@param {number} x
 * @return {boolean}* /
var isPalindrome = function(x) {
    const str = new String(x);
    let left = 0, right = str.length - 1;
    while(left < right) {
        if(str[left] ! == str[right])return false;
        left++;
        right--;
    }
    return true;
};
Copy the code

Method five: mathematical method, division and mod, order of magnitude

This method is essentially the same as method 2, except that it uses a mathematical order of magnitude to fetch digits instead of converting them to strings.

Use division and remainder to obtain the corresponding position of the number, no string operations.

  • First get the current order of magnitude n

  • throughx / n Get first place.
  • throughx % 10 Get the last bit.
  • (x % n) / 10 Remove the first and last bits, (x % n removes the first bits, x / 10 removes the last bits), and subtract 2 from the x bits.
  • n /= 100Minus 2 x bits, so n has to go into 10 to the 210

Taking 123421 as an example, the calculation process is as follows:

x n x/n x%10
– 123421. 100000 1 1
2342 100 2 2
34 10 3 4

Code:

/ * * *@param {number} x
 * @return {boolean}* /
var isPalindrome = function (x) {
    if (x < 0) return false;
    if (x < 10) return true;
    let n = 10 ** Math.floor(Math.log10(x));
    while (n > 1 && x > 0) {
        if (Math.floor(x / n) ! == x %10) return false;
        x = Math.floor((x % n) / 10);
        n /= 100;
    }
    return true;
};
Copy the code