- int 입력 x를 str로 형 변환 후, 문자열 slicing을 이용해 뒤집음.
- 뒤집을 시, 앞부분의 0을 제거하기 위해 결과값을 int로 변환함.
(ex. 120 -> 021 -> 21)- -2^31 ~ 2^31 밖의 값 일 경우 0을 return 해야 하므로 마지막에 확인해 줌.
class Solution:
def reverse(self, x: int) -> int :
if x >= 0 :
res = int(str(x)[::-1])
else :
res = '-' + str(int(str(abs(x))[::-1]))
if int(res) in range(pow(-2, 31), pow(2, 31)) :
return res
else :
return 0