LeetCode 题解工作台

反转两次的数字

反转 一个整数意味着倒置它的所有位。 例如,反转 2021 得到 1202 。反转 12300 得到 321 , 不保留前导零 。 给你一个整数 num , 反转 num 得到 reversed1 , 接着反转 reversed1 得到 reversed2 。如果 reversed2 等于 num …

category

1

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·driven

bolt

答案摘要

如果数字是 ,或者数字的个位不是 ,那么反转两次后的数字一定和原数字相等。 时间复杂度 ,空间复杂度 。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

反转 一个整数意味着倒置它的所有位。

  • 例如,反转 2021 得到 1202 。反转 12300 得到 321不保留前导零

给你一个整数 num反转 num 得到 reversed1接着反转 reversed1 得到 reversed2 。如果 reversed2 等于 num ,返回 true ;否则,返回 false

 

示例 1:

输入:num = 526
输出:true
解释:反转 num 得到 625 ,接着反转 625 得到 526 ,等于 num 。

示例 2:

输入:num = 1800
输出:false
解释:反转 num 得到 81 ,接着反转 81 得到 18 ,不等于 num 。 

示例 3:

输入:num = 0
输出:true
解释:反转 num 得到 0 ,接着反转 0 得到 0 ,等于 num 。

 

提示:

  • 0 <= num <= 106
lightbulb

解题思路

方法一:数学

如果数字是 00,或者数字的个位不是 00,那么反转两次后的数字一定和原数字相等。

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def isSameAfterReversals(self, num: int) -> bool:
        return num == 0 or num % 10 != 0
speed

复杂度分析

指标
时间complexity is O(log(num)) because the number of digits determines reversal operations. Space complexity is O(log(num)) if using string conversion or O(1) with arithmetic reversal.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Checks if you notice the impact of trailing zeros on digit reversal.

  • question_mark

    Tests your ability to reason about edge cases like zero and single-digit numbers.

  • question_mark

    Looks for a simple math-driven solution without unnecessary loops or sorting.

warning

常见陷阱

外企场景
  • error

    Assuming all numbers can be restored after double reversal, ignoring trailing zeros.

  • error

    Using arithmetic reversal without handling zero properly, causing incorrect comparison.

  • error

    Overcomplicating the solution instead of leveraging the math-driven pattern.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Instead of double reversal, ask if reversing three times returns the original number.

  • arrow_right_alt

    Extend to signed integers and determine if negative numbers behave similarly under double reversal.

  • arrow_right_alt

    Determine which numbers in a range are unchanged after a double reversal.

help

常见问题

外企场景

反转两次的数字题解:数学·driven | LeetCode #2119 简单