Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to verify palindromes in C++

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to verify palindromes on C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Palindrome Number validates palindromes

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

Example 1:

Input: 121

Output: true

Example 2:

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.

Example 3:

Input: 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

If the number of palindromes is converted into a string, it becomes the problem of verifying palindromes. It is no more difficult. Let's do follow up directly, not into strings, but directly operate on integers. You can use rounding and remainder to get the desired number, such as 1221. If you calculate 1221 / 1000, you can get the first digit 1, and if 1221 10, you can get the last 1. Make a comparison, and then take out the middle 22 to continue the comparison. The code is as follows:

Solution 1:

Class Solution {public: bool isPalindrome (int x) {if (x)

< 0) return false; int div = 1; while (x / div >

= 10) div * = 10; while (x > 0) {int left = x / div; int right = x% 10; if (left! = right) return false; x = (x% div) / 10; div / = 100;} return true;}}

Then look at a very ingenious solution, or first determine whether x is negative, here you can use a small trick, because the highest bit of an integer cannot be 0, so the lowest bit of palindromes can not be 0, except the number 0, so if you find that the end of a positive number is 0, you can also directly return to false. OK, let's take a look at the specific solution. To verify the number of palindromes, you need to see whether the front and back halves are symmetrical. If you flip the second half, you can see whether it is equal to the first half. So the way to do this is to take out the second half of the number and flip it. The specific method is to take out the lowest digit each time by taking a surplus of 10, and then add it to the end of the number taken out, that is, multiply revertNum by 10, plus this remainder, so that the flip is completed at the same time, and every time you take the lowest digit, x is divided by 10. So the loop stops when revertNum is greater than or equal to x. Because the digits of palindromes can be odd and even, if it is even, then revertNum should be equal to x; if it is odd, then the middle number is on the lowest bit of revertNum, and should be equal to x after dividing by 10. See the code as follows:

Solution 2:

Class Solution {public: bool isPalindrome (int x) {if (x)

< 0 || (x % 10 == 0 && x != 0)) return false; int revertNum = 0; while (x >

RevertNum) {revertNum = revertNum * 10 + x% 10; x / = 10;} return x = = revertNum | | x = = revertNum / 10;}}; this is the end of the content of "how to verify palindromes in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report