forked from sashaaero/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |