9. A palindrome

🌊

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: trueCopy the code

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.Copy the code

Example 3:

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

Example 4:

Input: x = -101 Output: falseCopy the code

Tip:


  • 2 31 Or less x Or less 2 31 1 -2^{31} \leq x \leq 2^{31} – 1

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

Double pointer 🔥

Their thinking

Int to char[]; int to char[]; int to char[]; Most people can think of this solution.

code

class Solution {
    public boolean isPalindrome(int x) {
        String number = Integer.toString(x);
        char[] chars = number.toCharArray();
        for (int i = 0; i <= (chars.length - 1) / 2; i++) {
            if(chars[i] ! = chars[chars.length - i -1]) {
                return false; }}return true; }}Copy the code

Submission result: Beats 52%

Mathematical Methods (advanced) 🔥

Their thinking

Do this by continually comparing the first (first) and last (last) digits of an integer, and then removing the first and last digits until the first and last digits are unequal (false) or the integer comparison is complete (true).

The method itself isn’t hard, but consider why the following program can effectively exclude numbers like 100221 and 100021.

// Try running a trace of the program in your mind for such numbers/Debug
while (x > 0) {
    if((x / divide) ! = (x %10))
    	return false;
    x = (x % divide) / 10;
    divide /= 100;
}
Copy the code

code

class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0)
            return false;
        int divide = 1;
        while (x / divide >= 10)
            divide *= 10;
        while (x > 0) {
            if((x / divide) ! = (x %10))
                return false;
            x = (x % divide) / 10;
            divide /= 100;
        }
        return true; }}Copy the code

Submitted results: Beats 78%

Semicircular inversion test (advanced) 🔥

Their thinking

code

class Solution {
    public boolean isPalindrome3(int x) {
        if (x < 0 || x % 10= =0&& x ! =0)
            return false;
        int halfReverse = 0;
        while (x > halfReverse) {
            halfReverse = halfReverse * 10 + x % 10;
            x /= 10;
        }
        return x == halfReverse || x == halfReverse / 10; }}Copy the code

Submission result: Beats 100%

📚 cheese

Double pointer: It refers to that two Pointers in the same direction (fast or slow Pointers) or opposite directions (colliding Pointers) are used to scan objects instead of a single pointer.

LeetCode there are a lot of questions can be solved by double pointer, including but not limited to: www.jianshu.com/p/2fab49be0…

The last 🍺

This is the fourth article in our “LeetCode” series, which began on 2021/08/31, with a total of 1949 titles in LeetCode as of the start date.

In this series of articles, in addition to clarifying the idea of solving the problem, step by step to give the optimal solution, and explore the cheese points involved/possibly related to the problem.

See the [LeetCode] series for more details