4、整数反转
class Solution:
def reverse(self, number:int) -> int:
max_int = 2**31 -1
mini_int = -2**31
if number < max_int and number >mini_int:
list_ = list(str(number))
list_.reverse()
if list_[-1] == "-":
return int("-" + "".join(list_[:-1]))
else:
return int("".join(list_))
5、把字符串转换成整数
class Solution:
def strToInt(self, str: str ) -> int:
if obj := re.search("^[-+]?[0-9]+", str.lstrip()):
number = int(obj.group())
if number > 2 ** 31 - 1:
return 2 ** 31 - 1
else if number < -2 ** 31:
return -2 ** 31
else:
return number
else:
return 0
6、回文数
class Solution:
def isPalindrome(self, x):
str_x = list(str(x))
i , j = 0 , len(str_x) - 1
while i < j :
if str_x[i] != str_x[j]:
return False
i += 1
j -=1
return True