Skip to content

Commit

Permalink
Add reverse-integer solution
Browse files Browse the repository at this point in the history
Leetcode id 7

Relates sashaaero#1
  • Loading branch information
Filipe Almeida committed Oct 10, 2019
1 parent 56738c8 commit dd21042
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions leet_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://leetcode.com/problems/reverse-integer
# Runtime: 12 ms, faster than 96.32% of Python online submissions for Reverse Integer.

class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
s = str(abs(x))

reversed = int(s[::-1])

if reversed > 2147483647:
return 0

return reversed if x > 0 else (reversed * -1)

if __name__ == '__main__':
s = Solution()
assert s.reverse(123) == 321

0 comments on commit dd21042

Please sign in to comment.