[LeetCode] 7.Reverse Integer (문자열 연산)

728x90
반응형

숫자를 문자열로 변환해서 뒤집은 다음 연산한다 

class Solution:
    def reverse(self, x: int) -> int:
        x=str(x)
        x=x[::-1]
        if x[-1]=='-':
            x=int('-'+x[:-1])
        x=int(x)
        if x>2**31-1 or x<(-2**31-1):
            return 0
        else:
            return x

 

728x90
반응형
TAGS.

Comments