In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces java how to achieve integer inversion, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.
Topic description
Given a 32-bit signed integer, you need to reverse the number on each of the integer.
Example 1:
Input: 123 output: 321
Example 2:
Input:-123 output:-321
Example 3:
Input: 120 output: 21
Note:
Assuming that our environment can only store 32-bit signed integers, the range of values is [− 231,231 − 1]. Based on this assumption, 0 is returned if the integer overflows after the inversion.
The idea of solving the problem
Tags: math
This question is very simple if the spillover problem is not taken into account. There are two ways to solve the overflow problem, the first is to solve the problem through string conversion plus try catch, and the second is to solve it through mathematical calculation.
Because the efficiency of string conversion is low and more library functions are used, the problem-solving scheme does not consider this method, but is solved by mathematical calculation.
Each bit of the number x is taken apart by a loop, and each step is determined whether or not to overflow when calculating the new value.
There are two overflow conditions, one is greater than the integer maximum value MAX_VALUE, the other is less than the integer minimum value MIN_VALUE, let the current calculation result is ans, the next bit is pop.
Judging from the overflow condition of ans * 10 + pop > MAX_VALUE
When ans > MAX_VALUE / 10 appears and there is still pop to be added, it must overflow
When ans = = MAX_VALUE / 10 and pop > 7, it must overflow. 7 is the single digit of 2 ^ 31-1.
From ans * 10 + pop
< MIN_VALUE这个溢出条件来看 当出现 ans < MIN_VALUE / 10 且 还有pop需要添加 时,则一定溢出 当出现 ans == MAX_VALUE / 10 且 pop < -8 时,则一定溢出,8是-2^31的个位数 代码class Solution { public int reverse(int x) { int ans = 0; while (x != 0) { int pop = x % 10; if (ans >Integer.MAX_VALUE / 10 | (ans = = Integer.MAX_VALUE / 10 & & pop > 7) return 0; if (ans < Integer.MIN_VALUE / 10 | | (ans = = Integer.MIN_VALUE / 10 & & pop <-8) return 0; ans = ans * 10 + pop; x / = 10;} return ans;}}
Thank you for reading this article carefully. I hope the article "how to achieve integer inversion of java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.