Skip to content

Commit

Permalink
Create reverse-int.py
Browse files Browse the repository at this point in the history
reverses integer without casting to string
  • Loading branch information
gabedonnan authored Jan 17, 2023
1 parent c75f669 commit 090529d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions reverse-int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def reverse(self, x: int) -> int:
rev = []#
switch = 1
if abs(x) != x:
switch = -1
x = abs(x)
final = 0
while x > 0:
rev.append(x % 10)#
x = x // 10
print(rev)
for i,num in enumerate(rev[::-1]):
final += num * (10 ** i)
final *= switch
if final > 2147483647:
return 0
elif final < -2147483648:
return 0
return final

0 comments on commit 090529d

Please sign in to comment.