class Solution:
def reverse(self, x: int) -> int:
intToString = str(x)
# zero check
if len(intToString) == 1:
return x
elif intToString[-1] == '0':
intToString = intToString[:-1]
# sign check
flag = False
if intToString[0] == '-':
flag = True
intToString = intToString[1:]
reverseList = reversed([intToString[idx] for idx in range(0, len(intToString))])
reverse = int(''.join(reverseList))
if reverse.bit_length() < 32:
if flag:
return reverse * -1
else:
return reverse
else:
return 0
반응형
'자료구조&알고리즘 > 알고리즘-문제풀이' 카테고리의 다른 글
Leetcode-easy-13. Roman to Integer (0) | 2020.07.15 |
---|---|
Leetcode-easy-9. Palindrome Number (0) | 2020.07.14 |
Leetcode-easy-1.Two Sum (0) | 2020.07.14 |
[프로그래머스-Level2] 프린터 (0) | 2020.06.24 |
[프로그래머스-Level1] 같은 숫자는 싫어 (0) | 2020.06.22 |