[Leetcode-09] 9. Palindrome Number

Time:2020-2-28

Subject requirements

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Determines whether an integer is a palindrome. Palindromes are integers that are read in the same positive (left to right) and reverse (right to left) order.
Link: https://leetcode-cn.com/probl

Example

Input: 121 Output: true

Input: -121 Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Input: 10 Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Train of thought

1. The most intuitive idea is to convert the string to reverse.
2. It is suggested that you do not use the method of string conversion, so the idea I come up with is to consider some boundary situations first, such as:

  • When this number is negative

If some boundary conditions are removed, the whole number is expected to be reversed. If the reversed number is the same as the original number, then this number is palindrome. For example, if we input 1221, we can reverse the whole number 1221 to 1221 and compare the two numbers. If they are the same, it is palindrome.

For the number 1221, you can first perform the operation of 1221% 10 to get 1, and then to get the penultimate number, you can divide 1221 by 10, and then find the remainder. At the same time, multiply the last number by 10 and add the penultimate number, then 1 * 10 + 2 = 12, you will get the desired result, and so on, you will get the reversed number.

The first is the first and second solutions

Java version

//Direct string inversion
class Solution {
    public boolean isPalindrome(int x) {
        String result = new StringBuilder(x + "").reverse().toString();
        return result.equals(String.valueOf(x));

    }
}

[Leetcode-09] 9. Palindrome Number

class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0 ) {
            return false;
        }
        int originX = x;
        int reverseNum = 0;
        while(x != 0) {
            reverseNum = reverseNum * 10 + x %10;
            x = x/ 10;
        }

        return originX == reverseNum;
    }
}

[Leetcode-09] 9. Palindrome Number

The official also gave an explanation
The official solution is to find only half of the number, for example, 1221, then find the lower part 12, find that it is equal to the upper part 12, and then exit, for example, 12321, then find the lower part 123, and find that it is 12 dozen, then exit, if it is non palindrome, then as long as the lower part is more than the upper part, you can exit the cycle.

However, I don’t think the official explanation is very well understood, but on his side, he considered that there would be overflow if it could be completely reversed. In terms of time efficiency, it’s not much different from the second one.

class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0 ) {
            return false;
        }

        if (x % 10 ==0 && x!=0) {
            return false;
        }

        int reverseNum = 0;
        while(x > reverseNum) {
            reverseNum = reverseNum * 10 + x %10;
            x = x/ 10;
        }

        return x == reverseNum || x == reverseNum/10;
    }
}

[Leetcode-09] 9. Palindrome Number

Recommended Today

Go doesn't need Java-style GC

This article was first published at https://robberphex.com/go-does-not-need-a-java-style-gc/. Modern languages ​​like Go, Julia, and Rust don't need as sophisticated garbage collectors as those used by Java c#. But why is this? We first need to understand how the garbage collector works and how the various languages ​​allocate memory differently. First, let's see why Java needs such […]