In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how C++ realizes flipping integers". In daily operation, I believe many people have doubts about how C++ realizes flipping integers. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how C++ realizes flipping integers"! Next, please follow the small series to learn together!
Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
The flip number problem needs attention is the overflow problem, read a lot of online solutions, because the previous OJ did not test the overflow, so many people on the Internet did not deal with the overflow problem can also pass the OJ. Now OJ has updated the overflow test, so it's worth considering. Why is there an overflow problem? Since the value range of int type is-2147483648 to 2147483647, if you want to flip 10000009, the number in the range will get 90000001, and the number after flipping will exceed the range. The initial idea of the blogger was to use long data, which ranged from-9223372036854775808 to 9223372036854775807, much larger than int data so that there would be no overflow problem. But in fact, the official solution given by OJ does not need to use long. It is more concise than his own. It does not specially deal with the sign. After careful consideration, the sign does not affect the calculation. Moreover, it does not use long data. It feels better to write, so post it:
Solution 1:
class Solution {public: int reverse(int x) { int res = 0; while (x != 0) { if (abs(res) > INT_MAX / 10) return 0; res = res * 10 + x % 10; x /= 10; } return res; }};
To check for overflow/underflow, we could check if ret > 214748364 or ret
< -214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why? (214748364 即为 INT_MAX / 10) 为什么不用 check 是否等于 214748364 呢,因为输入的x也是一个整型数,所以x的范围也应该在 -2147483648~2147483647 之间,那么x的第一位只能是1或者2,翻转之后 res 的最后一位只能是1或2,所以 res 只能是 2147483641 或 2147483642 都在 int 的范围内。但是它们对应的x为 1463847412 和 2463847412,后者超出了数值范围。所以当过程中 res 等于 214748364 时, 输入的x只能为 1463847412, 翻转后的结果为 2147483641,都在正确的范围内,所以不用 check。 我们也可以用 long 型变量保存计算结果,最后返回的时候判断是否在 int 返回内,但其实题目中说了只能存整型的变量,所以这种方法就只能当个思路扩展了,参见代码如下: 解法二: class Solution {public: int reverse(int x) { long res = 0; while (x != 0) { res = 10 * res + x % 10; x /= 10; } return (res >INT_MAX || res < INT_MIN) ? 0 : res; }}; At this point, the study of "how to achieve integer inversion in C++" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.