Leetcode7:整數反轉python實現
阿新 • • 發佈:2021-01-30
技術標籤:Leetcodepythonleetcode演算法
給你一個 32 位的有符號整數 x ,返回 x 中每位上的數字反轉後的結果。
如果反轉後整數超過 32 位的有符號整數的範圍 [−231, 231 − 1] ,就返回 0。
假設環境不允許儲存 64 位整數(有符號或無符號)。
class Solution:
def reverse(self, x: int) -> int:
result = int((str(x) if x > 0 else str(-x) + "-")[::-1])
return result if -2 ** 31 < result < 2 ** 31 - 1 else 0