문제 설명
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
예시
제한
- -231 <= x <= 231 - 1
풀이
class Solution:
def reverse(self, x: int) -> int:
answer = 0
if x < 0:
answer = -int(str(x)[1:][::-1])
else:
answer = int(str(x)[::-1])
if answer < -2**31 or answer > 2**31 - 1:
return 0
return answer
'Algorithm' 카테고리의 다른 글
[LeetCode/Python] Longest Common Prefix (0) | 2021.09.05 |
---|---|
[LeetCode/Python] Roman to Integer (0) | 2021.09.05 |
[프로그래머스 Level2] 소수 찾기 (0) | 2021.09.04 |
[프로그래머스 Level2] 구명보트 (0) | 2021.09.04 |
[프로그래머스 Level2] 최댓값과 최솟값 (0) | 2021.09.03 |