LeetCode 7. Reverse Integer

Created at 2018-01-04 Updated at 2018-01-04 Category LeetCode Tag LeetCode

Question

Given 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.

Solution

If the input is smaller than zero, make it positive.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
ans = 0
is_neg = x < 0
if is_neg:
x = -x
while x:
ans = ans * 10 + x % 10
x //= 10
if ans > 2**31-1 or ans < -2**31:
return 0
return -ans if is_neg else ans

Table of Content

  1. Question
  2. Solution
  3. Code
Site by GoingMyWay using Hexo & Random

Hide