[daily algorithm / brush through leetcode] 9 Palindromes (simple)

Time:2022-4-25

Click here to view more content related to algorithmic interview~

Title Description

Determine whether an integer is a palindrome number. Palindromes are integers that are read in the same order (from left to right) and reverse order (from right to left).

Example 1:

Input: 121 
Output: true

Example 2:

Input: - 121 
Output: false
Explanation: read from left to right, it is - 121. Read from right to left, 121 -. Therefore, it is not a palindrome number.

Example 3:

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

Advanced:

Can you solve this problem without converting integers to strings?


String solution

Since it is mentioned in the advanced level that it cannot be solved by string, Sanye will provide it.

class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x);
        StringBuilder sb = new StringBuilder(s);
        sb.reverse();
        return sb.toString().equals(s);
    }
}

Time complexity: the number of digits of the number n, which has about log10 (n) bits. The flip operation needs to execute a cycle. Complexity is O (log10 (n))

Space complexity: string is used as storage. Complexity is O (log10 (n))


Non string solution (full flip)

Original valuexThe value of does not exceed the representation range of int, but the inverted value will have the risk of overflow, so it is used herelongReceive, and finally compare whether they are equal.

class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        long ans = 0;
        int t = x;
        while (x > 0) {
            ans = ans * 10 + x % 10;
            x /= 10;
        }
        return ans - t == 0;
    }
}

Time complexity: DigitalnThe number is about $\ log {10} {n} $. Complexity is $\ log {10} {n}$

Space complexity: $o (1)$


Non string solution (partial flip)

If we add a familiar requirement in the advanced: only 32-bit signed integers can be stored in the environment.

Then we evenlongIt can’t be used. At this time, we should make full use of the characteristics of “palindrome”: the first half and the second half (flip) are equal.

The first half and the second half (turning over) here need to be discussed in different situations:

  • Palindrome length is odd: the palindrome center is an independent number, i.eAfter ignoring the palindrome center, the first half = = the second half (flip)。 Such as 1234321 palindrome string
  • Palindrome length is even: the palindrome center is in the middle of the two numbers, i.eFront half = = back half (flip)。 Such as 123321
class Solution {
    public boolean isPalindrome(int x) {
        //For negative numbers and numbers in x0, X00 and x000 formats, directly return flash
        if (x < 0 || (x % 10 == 0 && x != 0)) return false;
        int t = 0;
        while (x > t) {
            t = t * 10 + x % 10;
            x /= 10;
        }
        //Two cases of palindrome length: direct comparison & comparison by ignoring the center point (the last bit of T)
        return x == t || x == t / 10;
    }
}

Time complexity: DigitalnThe number is about $\ log {10} {n} $. Complexity is $\ log {10} {n}$

Space complexity: $o (1)$


last

This is the third in our “brush through leetcode” seriesNo.9The series began on January 1, 2021. As of the start date, there are 1916 questions on leetcode, some of which are locked questions. We will finish all the unlocked questions first.

In this series of articles, in addition to explaining the problem-solving ideas, we will also give the most concise code as far as possible. If the general explanation is involved, the corresponding code template will also be.

Since leetcode’s questions are increasing with the weekly and biweekly competitions, in order to facilitate us to count the progress, we will calculate the progress according to the total number of questions at the beginning of the series as the denominator and the completed questions as the numerator. Current progress is9/1916

In order to facilitate the students to debug and submit code on the computer, I have established a relevant warehouse: GitHub address & gitee address.

In the warehouse address, you can see the problem solution link of the series of articles, the corresponding code of the series of articles, the original problem link of leetcode and some other preferred problem solutions.

Algorithm and data structure
Leetcode problem solving
Algorithmic interview