LeetCode 题解工作台

整数反转

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。 如果反转后整数超过 32 位的有符号整数的范围 [−2 31 , 2 31 − 1] ,就返回 0。 假设环境不允许存储 64 位整数(有符号或无符号)。 示例 1: 输入: x = 123 输出: 321 示例 2: 输…

category

1

题型

code_blocks

9

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·driven

bolt

答案摘要

我们不妨记 和 分别为 和 $2^{31} - 1$,则 的反转结果 需要满足 $mi \le ans \le mx$。 我们可以通过不断地对 取余来获取 的最后一位数字 ,并将 添加到 的末尾。在添加 之前,我们需要判断 是否溢出。即判断 $ans \times 10 + y$ 是否在 $[mi, mx]$ 的范围内。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·driven 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

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

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

 

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

 

提示:

  • -231 <= x <= 231 - 1
lightbulb

解题思路

方法一:数学

我们不妨记 mimimxmx 分别为 231-2^{31}23112^{31} - 1,则 xx 的反转结果 ansans 需要满足 miansmxmi \le ans \le mx

我们可以通过不断地对 xx 取余来获取 xx 的最后一位数字 yy,并将 yy 添加到 ansans 的末尾。在添加 yy 之前,我们需要判断 ansans 是否溢出。即判断 ans×10+yans \times 10 + y 是否在 [mi,mx][mi, mx] 的范围内。

x>0x \gt 0,那么需要满足 ans×10+ymxans \times 10 + y \leq mx,即 ans×10+ymx10×10+7ans \times 10 + y \leq \left \lfloor \frac{mx}{10} \right \rfloor \times 10 + 7。整理得 (ansmx10)×107y(ans - \left \lfloor \frac{mx}{10} \right \rfloor) \times 10 \leq 7 - y

下面我们讨论上述不等式成立的条件:

  • ans<mx10ans \lt \left \lfloor \frac{mx}{10} \right \rfloor 时,不等式显然成立;
  • ans=mx10ans = \left \lfloor \frac{mx}{10} \right \rfloor 时,不等式成立的充要条件是 y7y \leq 7。如果 ans=mx10ans = \left \lfloor \frac{mx}{10} \right \rfloor 并且还能继续添加数字,说明此时数字是最高位,即此时 yy 一定不超过 22,因此,不等式一定成立;
  • ans>mx10ans \gt \left \lfloor \frac{mx}{10} \right \rfloor 时,不等式显然不成立。

综上,当 x>0x \gt 0 时,不等式成立的充要条件是 ansmx10ans \leq \left \lfloor \frac{mx}{10} \right \rfloor

同理,当 x<0x \lt 0 时,不等式成立的充要条件是 ansmi10ans \geq \left \lfloor \frac{mi}{10} \right \rfloor

因此,我们可以通过判断 ansans 是否在 [mi10,mx10][\left \lfloor \frac{mi}{10} \right \rfloor, \left \lfloor \frac{mx}{10} \right \rfloor] 的范围内来判断 ansans 是否溢出。若溢出,则返回 00。否则,将 yy 添加到 ansans 的末尾,然后将 xx 的最后一位数字去除,即 xx10x \gets \left \lfloor \frac{x}{10} \right \rfloor

时间复杂度 O(logx)O(\log |x|),其中 x|x|xx 的绝对值。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def reverse(self, x: int) -> int:
        ans = 0
        mi, mx = -(2**31), 2**31 - 1
        while x:
            if ans < mi // 10 + 1 or ans > mx // 10:
                return 0
            y = x % 10
            if x < 0 and y > 0:
                y -= 10
            ans = ans * 10 + y
            x = (x - y) // 10
        return ans
speed

复杂度分析

指标
时间O(\log(x))
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Expect discussion about handling overflow conditions explicitly.

  • question_mark

    Clarify whether negative numbers should retain their sign after reversal.

  • question_mark

    Look for solutions that avoid converting integers to strings.

warning

常见陷阱

外企场景
  • error

    Failing to check for 32-bit overflow before updating the reversed number.

  • error

    Incorrectly handling negative integers or losing the negative sign.

  • error

    Using string conversion which may exceed memory constraints or bypass math-driven strategy.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Reversing digits in a 64-bit integer while ensuring 64-bit overflow is handled.

  • arrow_right_alt

    Counting how many digits can be reversed before reaching overflow.

  • arrow_right_alt

    Reversing only the even or odd digits of a number while preserving their positions.

help

常见问题

外企场景

整数反转题解:数学·driven | LeetCode #7 中等