- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
Latest commit
23 lines (18 loc) · 498 Bytes
/
Copy path7.py
File metadata and controls
23 lines (18 loc) · 498 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Name: Reverse Integer
# Link: https://leetcode.com/problems/reverse-integer/
# Method: Pop via mod, build new int
# Time: O(log(n))
# Space: O(1)
# Difficulty: Easy
classSolution:
defreverse(self, x: int) ->int:
sign=-1if (x<0) else1
x=abs(x)
new_nr=0
whilex!=0:
digit=x%10
new_nr=new_nr*10+digit
x//=10
ifnew_nr>2**31:
return0
returnnew_nr*sign