LeetCode 筆記 - 7. Reverse Integer Posted on 2022-05-09題目在此 7. Reverse Integer將數字反轉解題思維給定一個數字,將數字反轉。Time complexity: O(1)程式碼123456789101112131415161718192021class Solution: def reverse(self, x: int) -> int: x = str(x) if len(x) < 2: return x if x.startswith('-'): x = x[::-1] x = f'-{x[:-1]}' else: x = x[::-1] x = int(x) # overflow if x >= 2147483651: return 0 if x <= -2147483651: return 0 return x也許你也會想看看LeetCode 筆記 - 9. Palindrome NumberLeetCode 筆記 - 344. Reverse StringLeetCode 筆記 - 733. Flood Fill