Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities.
I. Preface:
π¨π Author: Bug Bacteria
βοΈ blog: CSDN, Nuggets, etc
π public account: Magic House of the Circle of the Apes
π« special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.
π Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.
Hello, little friends, I am bug bacteria π. Gold three silver four, brush the month again. So whether you’re looking for a career change or a career change, get your act together and do the right thing π£. So, quickly follow the pace of bug bacteria roll up β°, strong from this moment! β π§
In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles π. Your likes (collect βοΈ+ pay attention to π¨ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon ππ»βοΈ, nuggets stop π, cheer up π»
Ii. Title Description:
Difficulty: β β
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
It’s a palindrome, and123
It isn’t.
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
Title: LeetCode website
Iii. Analysis of Ideas:
Idea 1: Turn to string method
The best thing to do, as you can imagine, is to turn the integer into a string, then split the string into an array, and then loop through half the length of the array to see if the elements are equal.
Idea 2: Number flip method
When we look at palindromes intuitively, it’s like folding numbers in half to see if they match. So the operation of this solution is to take the last part of the number and flip it.
One point that needs to be noted here is that the number of palindromes can be even and odd, so please see the following:
- When its length is even, it has to be equal when folded in half;
- When it has an odd length, then when it’s folded in half, the longer half needs to get rid of the last digit, because it’s flipped, so get rid of the last digit, which is the first digit in the second half.
Iv. Algorithm implementation:
Turn string method _AC code
The specific implementation code is as follows:
Class Solution {public Boolean isPalindrome(int x) {public Boolean isPalindrome(int x) {String STR = string.valueof (x); For (int I = 0; i < str.length() / 2; If (str.charat (I)!) {// If (str.charat (I)! = str.charAt(str.length() - i - 1)) { return false; } } return true; }}Copy the code
Number flipping method _AC code
The specific implementation code is as follows:
Class Solution {public Boolean isPalindrome(int x) {public Boolean isPalindrome(int x) {class Solution {public Boolean isPalindrome(int x) { if (x < 0 || (x % 10 == 0 && x ! = 0)) { return false; } // revertNum = 0; While (x > revertNum) {revertNum = revertNum * 10 + x % 10; x /= 10; } return x == revertNum || x == revertNum / 10; }}Copy the code
V. Summary:
The results of leetcode submission are as follows:
The results of leetcode submission are as follows:
To sum up, the two methods have their own angles. Although the first way is often thought of, it is best to solve the problem according to one way. However, we still need to make a comparison. The second idea is to directly make use of the characteristics of palindromes. We only need to take a half-flip comparison and then start from a mathematical perspective to intercept numbers, which obviously improves the operation efficiency of the algorithm.
. .
Furthermore, there are thousands of ways to solve the problem. If you have any better ideas or ideas, please let me know in the comment section. We can learn from each other and grow faster.
Well, that’s all for this episode and I’ll see you next time.
Six, the previous recommendation:
- Leetcode -1. Sum of two numbers
- Leetcode – 9. Palindrome
. .
Vii. Finally:
If you want to learn more, check out bug Bug’s daily Question LeetCode! Take you to brush together. One person may feel very tired and difficult to persist in brushing, but a group of people will think it is a meaningful thing to brush, urge and encourage each other, and become stronger together.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
βοΈ Be who you want to be, there is no time limit, you can start whenever you want,
π You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
β
π If this article is helpful to you, please leave a like! (# ^. ^ #);
π if you like the article shared by bug fungus, please give bug fungus a point of concern! The danjun ‘α΄, you guys will have a cameo appearance with you.
π if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];
π In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);
π copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you