Likou Brushing Hundred Days Plan Day5 Integer Reversal

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 7 Official Link


Learning Content:

Integer inversion

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [231,  2311] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

Example 1:

Input: x = 123
Output: 321
Example 2:

Input: x = -123
Output: -321
Example 3:

Input: x = 120
Output: 21
Example 4:

Input: x = 0
Output: 0

提示:

-231 <= x <= 231 - 1

Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-integer

study-time:

2022.1.11


Learning output:

idea one
insert image description here

Problem solving ideas

1. If we want to reverse the integers, we only need to output them in reverse order. Naturally, we think of using strings to store numbers, and then traversing the array in reverse order, but this complicates the problem. It took a lot of time to do this before, but looking back, it didn't have to be so complicated at all. So, think more!
2. We can take the remainder of 10 each time, and the result of the remainder is exactly the last digit, that is, the first digit we want to output in reverse order, and then divide the incoming x by 10, discarding the last digit Okay, when x becomes 0, that is, all
3 are taken out. We can traverse
4 by writing a while loop. But there is a very pit point, the maximum value of Int data is 2 to the 31st power -1, The minimum value is negative 2 to the 31st power, however, we can't compare the value with it, because Int can't store its value. Therefore, when we reach the ninth place, we have to make a judgment. If the first nine places are greater than it, then the last place does not need to judge at all, and directly crosses the line. If the first nine digits are equal to it, then it will be judged for a long time whether the last digit we took out is greater than the maximum value, and the value exceeds the bounds.

public class Solution {
    
    
    public int Reverse(int x) {
    
    
        int res = 0;
            int currentNumber = 0;
            while (x != 0)
            {
    
    
                currentNumber = x % 10;
                //为什么我们这里要比较前九位?而不是直接比较十位?因为十位会直接越界,所以比较前九位就行 
                if (res > 214748364 || (res == 214748364 && currentNumber > 7))
                {
    
    
                    return 0;
                }
                
                if (res < -214748364 || (res == -214748364 && currentNumber < -8))
                {
    
    
                    return 0;
                }

                res = res * 10 + currentNumber;
                x /= 10;
            }

            return res;
    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122443344