문제
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
풀이
class Solution:
def reverse(self, x: int) -> int:
if (x == 0) :
return 0
if (x < 0) :
neg = True
else :
neg = False
numStr = str(abs(x))
numStr = numStr[::-1]
if (neg == True) :
result = int('-' + numStr)
else :
result = int(numStr)
max_result = pow(2,31)
if ( -max_result < result < max_result - 1) :
return result
else :
return 0
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
20. Valid Parentheses (0) | 2020.05.04 |
---|---|
9. Palindrome Number (0) | 2020.05.04 |
1. Two Sum (0) | 2020.05.03 |
14.Longest Common Prefix (0) | 2020.05.03 |
13. Roman to Integer (0) | 2020.05.02 |