题61(中等):
分析:
python代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
#利用空间复杂度解决,将链表装进列表
node_list=[]
p=head
while p!=None:
node_list.append(p)
p=p.next
node_len=len(node_list)
if node_len==0:
return head
k_new=k%node_len
if k_new==0:
return head
node_list[-1].next=node_list[0]
head=node_list[-k_new]
node_list[-(k_new+1)].next=None
return head
题62(中等):
分析:
python代码:
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
#思路就是动态数组嘛
auto_list=[[1 for j in range(n)] for i in range(m)]
for i in range(1,m):
for j in range(1,n):
a1,a2=0,0
if i-1>=0:
a1=auto_list[i-1][j]
if j-1>=0:
a2=auto_list[i][j-1]
auto_list[i][j]=a1+a2
return auto_list[-1][-1]
题63(中等):
分析
会这个差不多可以制作迷宫小游戏了
python代码:
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
#动态数组,和上一题差不多
#构造动态数组,obstacleGrid中有障碍为设置为0,没有设置为1
auto_list=[[0 for i in range(len(obstacleGrid[0]))] for j in range(len(obstacleGrid))]
for i in range(len(obstacleGrid)):
for j in range(len(obstacleGrid[0])):
if obstacleGrid[i][j]==0:
auto_list[i][j]=1
else:
auto_list[i][j]=0
#设置动态数组的值
for i in range(len(obstacleGrid)):
for j in range(len(obstacleGrid[0])):
#第一个位置必须是0
if i==0 and j==0:
continue
if auto_list[i][j]==0:
continue
a1,a2=0,0
if i-1>=0:
a1=auto_list[i-1][j]
if j-1>=0:
a2=auto_list[i][j-1]
auto_list[i][j]=a1+a2
return auto_list[-1][-1]
题64(中等):
分析:
python代码:
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
#动态规划,本质上和前面的题目是一样的
auto_list=grid.copy()
for i in range(len(auto_list)):
for j in range(len(auto_list[0])):
if i==0 and j==0:
#第一个就跳过就好了
continue
if i-1<0:
auto_list[i][j]+=auto_list[i][j-1]
continue
if j-1<0:
auto_list[i][j]+=auto_list[i-1][j]
continue
auto_list[i][j]+=min(auto_list[i-1][j],auto_list[i][j-1])
return auto_list[-1][-1]
题65(困难):
分析:
不得不说,这个有效数字判定规则很诡异,不过难度真不配困难
python代码:
class Solution:
def isNumber(self, s: str) -> bool:
s_stack=[]
dot_flag=0
sign_flag=0
e_flag=0
for i in s:
#如果为数字
if i in ('0','1','2','3','4','5','6','7','8','9'):
if dot_flag==1:
dot_flag=0
if e_flag==1:
e_flag=0
if sign_flag==1:
sign_flag=0
s_stack.append(i)
#如果为.
elif i=='.':
if s_stack==[] or s_stack==['+'] or s_stack==['-']:
dot_flag=1
if '.' in s_stack:
return False
if ('e' in s_stack) or ('E' in s_stack):
return False
s_stack.append('.')
#如果为+-
elif i=='+' or i=='-':
#如果不是空或者前面不是e就0
if s_stack==[]:
sign_flag=1
s_stack.append(i)
elif s_stack[-1]=='e' or s_stack[-1]=='E':
s_stack.append(i)
else:
return False
#如果为e或者E
elif i=='e' or i=='E':
print(s_stack)
e_flag=1
if 'e' in s_stack or 'E' in s_stack:
return False
if s_stack==[] or s_stack==['+'] or s_stack==['-']:
return False
if s_stack==['.'] or s_stack==['+','.'] or s_stack==['-','.']:
return False
s_stack.append(i)
else:
return False
if dot_flag==1:
return False
if e_flag==1:
return False
if sign_flag==1:
return False
return True