7.整数翻转
class Solution:
def __reversed__(self, x:int)->int:
int_max = 2**31-1
int_min = -(2**31)
rev = 0
while x:
pop = x%10 if x>0 else x%-10
x = x//10 if x>0 else int(x/10)
if rev>int_max or (rev == int_max and pop>7):
return 0
if rev<int_min or (rev ==int_min and pop <-8):
return 0
return rev
- 字符串转换整数 (atoi)
class Solution:
def myAtoi(self, s: str) -> int:
return max(min(int(*re.findall('^[\+\-]?\d+', s.lstrip())), 2**31 - 1), -2**31)
9.回文数
class Solution:
def isPalindrome(self, x: int) -> bool:
return True if str(x) == str(x)[::-1] else False