Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Spring Recruit punch card day 10 chapter 13.

Study like the seedlings of spring, see its increase, day has a director; Drop out of school such as a whetstone, see its loss, loss.

There are so many activities in digging gold. This month, I decided to use GO to brush the questions every day, on the one hand, to improve the algorithm level, on the other hand, to settle the learning of GO language.

Let’s GO!

Topic describes

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.

The sample

Example 1:

Enter: x = 121

Output: true,

Example 2:

Enter: x = -121

Output: false

Interpretation: Read from left to right as -121. Read from right to left, 121-. So it’s not a palindrome number.

Example 3:

Enter: x = 10

Output: false

Interpretation: Read from right to left as 01. So it’s not a palindrome number.

Tip:

-231 <= x <= 231 – 1  

Advanced:

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

Their thinking

  1. According to the characteristics of palindrome numbers, we can know that a negative number or similar 10, 100, 1000 is definitely not a palindrome number
  2. We can use the idea of reverse comparison to solve the problem. That is, reverse the last half of the number and compare it with the first half
  3. Note the cases where the first half and the second half are exactly equal (1221) or one place apart (121)

AC code

func isPalindrome(x int) bool {
    Return false if it is 10, 100, 1000, or negative
    if x < 0|| (x ! =0 && x %10= =0) {
        return false
    }
    // Reverse the comparison
    var right int
    for x > right{
        right = right*10 + x%10
        x /= 10
    }
  
    return x == right || x == right/10  
}
Copy the code

The results

conclusion

“Palindrome” refers to the sentence that can be read both forward and backward. It is a kind of rhetoric and word game in ancient and modern times, such as “one for all, all for one” and so on. In mathematics, there is also a class of numbers with this characteristic, called the Palindrome number.

sources

Source: LeetCode

Link: leetcode-cn.com/problems/pa…

Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!