007 整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
class Solution:
def reverse(self, x: int) -> int:
s = str(x)
if s[0]=='-':
x = int('-'+s[1:][::-1])
else:
x = int(s[::-1])
return x if -2**31<x<2**31-1 else 0
008 字符串转换整数
class Solution:
def myAtoi(self, str: str) -> int:
i=0
n=len(str)
while i<n and str[i]==' ':
i=i+1
if n==0 or i==n:
return 0
flag=1
if str[i]=='-':
flag=-1
if str[i]=='+' or str[i]=='-':
i=i+1
INT_MAX=2**31-1
INT_MIN=-2**31
ans=0
while i<n and '0'<=str[i]<='9':
ans=ans*10+int(str[i])-int('0')
i+=1
if(ans-1>INT_MAX):
break
ans=ans*flag
if ans>INT_MAX:
return INT_MAX
return INT_MIN if ans<INT_MIN else ans
009 回文数
class Solution:
def isPalindrome(self, x: int) -> bool:
s = str(x)
i,j = 0,len(s)-1
if s[0] =='-':
return False
while i<= j:
if s[i]!=s[j]:
return False
i += 1
j -= 1
return True