Q7. Reverse Integer

直达:https://leetcode.com/problems/reverse-integer/description/

iven a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note: Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

分析

保存正负号后使用绝对值进行运算。

使用 %10取个位,使用 /10进行右移1位。

使用长整型保存结果(Python不用)以便进行溢出判断。

代码

C++

python

Last updated

Was this helpful?