LeetCode/릿코드-Reverse Integer-python

4655 단어 leetcodeleetcode

문제

풀이

  • given a signed 32 - bit inger x, return x with its digits reversed.
  • if reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], them return 0.
  • assume the environment does not allow you to store 64-bit integers.

코드

# leetcode, easy : reverse integer, python3
# math 
class Solution:
    def reverse(self, x: int) -> int:
        x = str(x)
        
        if x[0] == '-':
            x = ''.join(list(reversed(x[1:])))
            result = int('-' + x)
        else:
            result = int(''.join(list(reversed(x))))
            
        if result > 2**31 - 1 or result < -2**31:
            return 0
            
        return int(result)

결과



출처 && 깃허브

LeetCode
github

좋은 웹페이지 즐겨찾기