Integer inversion LeetCode 07

Integer inversion


Topic link:

https://leetcode-cn.com/problems/reverse-integer/

Title description

Given a 32-bit signed integer, you need to invert the digits on each of the integers.

Note: Assuming that our environment can only store 32-bit signed integers, the value range is [−2^31, 2 ^ 31 − 1]. According to this assumption, if the integer overflows after the inversion, it returns 0.

Example:

Example 1:
Input: 123
Output: 321


Example 2:
Input: -123
Output: -321


Example 3:
Input: 120
Output: 21

Detailed topic

This question requires us to invert a signed integer, that is to say, the number 64 will become 46 after inversion, the ones digit is inverted to the tens digit, and the tens digit is inverted to the single digit. If the given integer is negative, the sign of the integer cannot be changed. We should pay attention to the following points in this question:

  • Integers will have negative numbers;
  • The signed integer required in the question is a 32-bit integer (the value range of a 32-bit integer: -2147483648~2147483647, beyond this range is an overflow), if an overflow occurs after the integer is inverted, then 0 should be returned;
  • If the last bit is 0, it should be discarded. For example, after 120 is inverted, it becomes 21.

Problem solving plan

Idea 1: Time complexity: O(lgx) Space complexity: O(1)

What needs attention when flipping numbers is the overflow problem. Why is there an overflow problem?

The value range of our int type is -2147483648~2147483647 (-2^31 ~ 2^31-1) , then if we want to flip the number within the range of 1000000003 , we get 3000000001 , but the number after the flip exceeds the range , This situation is overflow, at this time the program returns 0.

If the input is a negative number, the original function is called recursively, and the parameter becomes -x. The approximate execution flow of the code is as follows:

  1. First judge whether x is a negative number, if it is a negative number, first take the absolute value and then recursively reverse, and finally convert the result to a negative number;
  2. res is the final result, initially equal to 0. x% 10 (for example: 123% 10 = 3) Take the last digit. Get the last digit each time and use it as the current highest digit in the result;
  3. Determine whether the result res is overflow, and return 0 if it overflows.

The detailed code is as follows:

public class Solution {
    
    

    /**
     * 时间复杂度: O(lgx) 空间复杂度: O(1)
     * @param x 目标值
     * @return int
     */
    public static int reverse(int x) {
    
    
        if (x == -2147483648) {
    
     //如果不做这个判断,下面的x=-x将会报错
            return 0;
        }

        if (x < 0) {
    
     // 判断是否为负数
            return -reverse(-x); // 如果是负数则取绝对值调用自身,最后将结果转为负数
        }

        int res = 0;
        while (x != 0) {
    
     // 每次得到最后一位数字,并将其作为结果中的当前最高位
            if (res > 214748364) {
    
     // 处理溢出
                return 0;
            }
            res = res * 10 + x % 10;
            x /= 10;
        }
        return res <= Integer.MAX_VALUE ? res : 0; // 如果溢出就返回0
    }
}

Guess you like

Origin blog.csdn.net/weixin_38478780/article/details/108419439