当前位置: 首页 > news >正文

力扣61~65题

题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=headwhile p!=None:node_list.append(p)p=p.nextnode_len=len(node_list)if node_len==0:return headk_new=k%node_lenif k_new==0:return headnode_list[-1].next=node_list[0]head=node_list[-k_new]node_list[-(k_new+1)].next=Nonereturn 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,0if i-1>=0:a1=auto_list[i-1][j]if j-1>=0:a2=auto_list[i][j-1]auto_list[i][j]=a1+a2return auto_list[-1][-1]

题63(中等):

分析

会这个差不多可以制作迷宫小游戏了

python代码:

class Solution:def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:#动态数组,和上一题差不多#构造动态数组,obstacleGrid中有障碍为设置为0,没有设置为1auto_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]=1else:auto_list[i][j]=0#设置动态数组的值for i in range(len(obstacleGrid)):for j in range(len(obstacleGrid[0])):#第一个位置必须是0if i==0 and j==0:continueif auto_list[i][j]==0:continuea1,a2=0,0if i-1>=0:a1=auto_list[i-1][j]if j-1>=0:a2=auto_list[i][j-1]auto_list[i][j]=a1+a2return 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:#第一个就跳过就好了continueif i-1<0:auto_list[i][j]+=auto_list[i][j-1]continueif j-1<0:auto_list[i][j]+=auto_list[i-1][j]continueauto_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=0sign_flag=0e_flag=0for i in s:#如果为数字if i in ('0','1','2','3','4','5','6','7','8','9'):if dot_flag==1:dot_flag=0if e_flag==1:e_flag=0if sign_flag==1:sign_flag=0s_stack.append(i)#如果为.elif i=='.':if s_stack==[] or s_stack==['+'] or s_stack==['-']:dot_flag=1if '.' in s_stack:return Falseif ('e' in s_stack) or ('E' in s_stack):return Falses_stack.append('.')#如果为+-elif i=='+' or i=='-':#如果不是空或者前面不是e就0if s_stack==[]:sign_flag=1s_stack.append(i)elif s_stack[-1]=='e' or s_stack[-1]=='E':s_stack.append(i)else:return False#如果为e或者Eelif i=='e' or i=='E':print(s_stack)e_flag=1if 'e' in s_stack or 'E' in s_stack:return Falseif s_stack==[] or s_stack==['+'] or s_stack==['-']:return Falseif s_stack==['.'] or s_stack==['+','.'] or s_stack==['-','.']:return Falses_stack.append(i)else:return Falseif dot_flag==1:return Falseif e_flag==1:return  Falseif sign_flag==1:return Falsereturn True

http://www.lryc.cn/news/463596.html

相关文章:

  • API接口开发流程与指南
  • 如何在Android中进行日志打印和调试?
  • Linux基本使用和程序部署
  • 照片编辑成动态视频用什么软件好
  • JavaWeb合集-SpringBoot项目配套知识
  • Electron入门笔记
  • python 不相交集简介(并查集算法)【Introduction to Disjoint Set (Union-Find Algorithm)】
  • 23种设计模式之工厂方法模式
  • Redis——事务
  • Redis非关系型数据库操作命令大全
  • 基于SpringBoot+Vue+uniapp微信小程序的澡堂预订的微信小程序的详细设计和实现
  • Linux mips架构链接库函数调用plt表汇编代码分析
  • python 作业1
  • Apache 出现 “403 forbidden“ 排查方法
  • vue video播放m3u8监控视频
  • uniapp 获取签名证书 SHA1 自有证书签名打包
  • Open3d开发点云标注工具问题总结(二)
  • 【FreeRTOS】
  • 洛谷 P4995:跳跳! ← 贪心算法
  • 代理 IP 在 AI 爬虫中的关键应用
  • 【Vercel】Vercel静态部署踩坑
  • 【Spring】关于Spring中aware相关接口的作用
  • 动态内存管理及RAII的简单应用
  • 7、Vue2(一)
  • Chapter11
  • LLAMA2入门(一)-----预训练
  • 使用poi-tl动态写入目录更新问题解决
  • OpenCV高级图形用户界面(9)更改指定窗口的位置函数moveWindow()的使用
  • 华山论剑之Rust的Trait
  • AI 编译器学习笔记之七五 -- pdb 使用方法