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

LeetCode 每日一题 2023/8/14-2023/8/20

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • 8/14 617. 合并二叉树
      • 8/15 833. 字符串中的查找与替换
      • 8/16 2682. 找出转圈游戏输家
      • 8/17 1444. 切披萨的方案数
      • 8/18 1388. 3n 块披萨
      • 8/19 2235. 两整数相加
      • 8/20


8/14 617. 合并二叉树

dfs深搜

class TreeNode(object):def __init__(self, val=0, left=None, right=None):self.val = valself.left = leftself.right = right
def mergeTrees(root1, root2):""":type root1: TreeNode:type root2: TreeNode:rtype: TreeNode"""def func(n1,n2):if not n1:return n2if not n2:return n1node = TreeNode(n1.val+n2.val)node.left = func(n1.left,n2.left)node.right = func(n1.right,n2.right)return nodereturn func(root1,root2)

8/15 833. 字符串中的查找与替换

op存放该位置能替换的数值
从头遍历每个位置

def findReplaceString(s, indices, sources, targets):""":type s: str:type indices: List[int]:type sources: List[str]:type targets: List[str]:rtype: str"""from collections import defaultdictn = len(s)op = defaultdict(list)for i,ind in enumerate(indices):op[ind].append(i)ans = []i = 0while i<n:tag = Falseif i in op:for ind in op[i]:if s[i:i+len(sources[ind])]==sources[ind]:tag = Trueans.append(targets[ind])i+=len(sources[ind])breakif not tag:ans.append(s[i])i+=1return "".join(ans)

8/16 2682. 找出转圈游戏输家

模拟

def circularGameLosers(n, k):""":type n: int:type k: int:rtype: List[int]"""do = [False]*ncur = 0i=1while not do[cur]:do[cur]=Truecur+=i*kcur%=ni+=1return [i+1 for i in range(n) if not do[i]]

8/17 1444. 切披萨的方案数

动态规划 dp[k][i][j] 表示把坐标(i,j)右下方切割成k块的方案

def ways(pizza, k):""":type pizza: List[str]:type k: int:rtype: int"""mod = 10**9+7m,n=len(pizza),len(pizza[0])apples = [[0]*(n+1) for _ in range(m+1)]dp = [[[0 for j in range(n)] for i in range(m)] for _ in range(k+1)]for i in range(m-1,-1,-1):for j in range(n-1,-1,-1):apples[i][j] = apples[i][j+1]+apples[i+1][j]-apples[i+1][j+1]+(pizza[i][j]=='A')if apples[i][j]>0:dp[1][i][j] = 1 else:dp[1][i][j] = 0for t in range(1,k+1):for i in range(m):for j in range(n):for ii in range(i+1,m):if apples[i][j]>apples[ii][j]:dp[t][i][j] = (dp[t][i][j]+dp[t-1][ii][j])%modfor jj in range(j+1,n):if apples[i][j]>apples[i][jj]:dp[t][i][j] = (dp[t][i][j]+dp[t-1][i][jj])%modreturn dp[k][0][0]

8/18 1388. 3n 块披萨

可转换为在3n个数中 选择n个不相邻的数 和最大
动态规划dp[i][j]表示前i个数选择j个不相邻的数 最大和

def maxSizeSlices(slices):""":type slices: List[int]:rtype: int"""def func(slices):m = len(slices)n = (len(slices)+1)//3dp = [[float("-inf") for _ in range(n+1)] for _ in range(m)]dp[0][0] = 0dp[0][1] = slices[0]dp[1][0] = 0dp[1][1] = max(slices[0],slices[1])for i in range(2,m):dp[i][0] = 0for j in range(1,n+1):dp[i][j] = max(dp[i-1][j],dp[i-2][j-1]+slices[i])return dp[m-1][n]return max(func(slices[1:]),func(slices[0:-1]))

8/19 2235. 两整数相加

如题相加

def sum(num1, num2):""":type num1: int:type num2: int:rtype: int"""return num1+num2

8/20


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

相关文章:

  • 进入微服务阶段后的学习方法
  • C/C++中const关键字详解
  • 【2023新教程】树莓派4B开机启动-树莓派第一次启动-树莓派不使用显示器启动-树莓派从购买到启动一步一步完全版!
  • LA@2@1@线性方程组和简单矩阵方程有解判定定理
  • 如何使用ChatGPT创作一个小说式的虚构的世界
  • 用于量子通信和互联网的光量子芯片
  • 11. Vuepress2.x 关闭夜间模式
  • netty实现websocket通信
  • 两个list如何根据一个list中的属性去过滤掉另一个list中不包含这部分的属性,用流实现
  • Blender 混合现实3D模型制作指南【XR】
  • kubeasz在线安装K8S集群单master集群(kubeasz安装之二)
  • 『C语言』数据在内存中的存储规则
  • 基于ssm+vue的新能源汽车在线租赁管理系统源码和论文PPT
  • 深入解析IDS/IPS与SSL/TLS和网络安全
  • 在Visual Studio上,使用OpenCV实现人脸识别
  • 搭建openGauss 5.0 一主一从复制集群
  • Docker碎碎念
  • 【C++】extern
  • 2023全网Mysql 合集(25w字)附课程 从安装到高级,实战
  • 张俊林:由ChatGPT反思大语言模型(LLM)的技术精要
  • 单机编排docker compose
  • C++ 面向对象三大特性——多态
  • 相同数字的积木游戏
  • 安防监控视频云存储EasyCVR平台H.265转码功能更新:新增分辨率配置
  • 图数据库_Neo4j学习cypher语言_常用函数_关系函数_字符串函数_聚合函数_数据库备份_数据库恢复---Neo4j图数据库工作笔记0008
  • LeetCode150道面试经典题-- 加一(简单)
  • Centos7 配置Docker镜像加速器
  • 微信小程序中pdf的上传、下载及excel导出
  • Python_11 类的方法
  • CentOS系统环境搭建(一)——Centos7更新