LeetCode 每日一题 2025/1/27-2025/2/2

embedded/2025/2/7 14:41:57/

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


目录

      • 1/27 45. 跳跃游戏 II
      • 1/28 119. 杨辉三角 II
      • 1/29 219. 存在重复元素 II
      • 1/30 350. 两个数组的交集 II
      • 1/31 541. 反转字符串 II
      • 2/1 81. 搜索旋转排序数组 II
      • 2/2 598. 区间加法 II


1/27 45. 跳跃游戏 II

题目说明一定可以跳到队末 要次数最少
找到跳到队尾的最靠前的一个点 将队尾移到这个点 跳动次数+1 pos记录当前的地址
反复寻找
begin用来记录重新开始寻找的点
如果开头是1则直接进行跳跃 次数+1 begin+1 及略过开头若干个1

python">def jump(nums):""":type nums: List[int]:rtype: int"""begin = 0pos = 0 num = 0l = len(nums)if l<2:return numwhile True:while nums[pos]==1:pos+=1if pos==l-1:breaknum+=1begin = poswhile pos+nums[pos]<l-1:pos+=1num +=1if pos==begin or pos==l-1:breakl = pos+1pos = beginreturn num

1/28 119. 杨辉三角 II

按规则生成

python">def getRow(rowIndex):""":type rowIndex: int:rtype: List[int]"""if rowIndex==0:return [1]elif rowIndex==1:return [1,1]pre = [1,1]ind = 1while ind<rowIndex:ind+=1cur = [1]for i in range(len(pre)-1):cur.append(pre[i]+pre[i+1])cur.append(1)pre=cur[:]return cur

1/29 219. 存在重复元素 II

滑动窗口 保持k+1长度 记录长度内出现的数字个数
如果数字个数大于1 则成立

python">def containsNearbyDuplicate( nums, k):""":type nums: List[int]:type k: int:rtype: bool"""from collections import defaultdictwin = defaultdict(int)for i in range(min(len(nums),k+1)):win[nums[i]]+=1if win[nums[i]]>1:return Truefor i in range(k+1,len(nums)):win[nums[i-k-1]]-=1win[nums[i]]+=1if win[nums[i]]>1:return Truereturn False

1/30 350. 两个数组的交集 II

ans1: 接349 获取不重复的交集l
遍历l中的元素
找到两个集合中该元素出现最少的次数 添加进答案
ans2: 将第一个集合编成 字符-次数 的字典 遍历第二个集合
如果某一个字符在字典中并且次数不为0 则加入 字典中的次数减一

python">from collections import defaultdictdef intersect(nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: List[int]"""n1=set(nums1)n2=set(nums2)l = list(n1&n2)res=[]for x in l:num = min(nums1.count(x),nums2.count(x))res.extend([x]*num)return resdef intersect2(nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: List[int]"""d = defaultdict(int)res=[]for i in nums1:d[i]+=1for i in nums2:if d[i]>0:res.append(i)d[i]-=1return res

1/31 541. 反转字符串 II

l,r代表需要反正的字符段左右
l需要小于s的长度 r为l+k-1和长度n-1之间的较小值
下一个l为之前r+k+1

python">def reverseStr(s, k):""":type s: str:type k: int:rtype: str"""n = len(s)ls= list(s)l = 0while l<n:r = min(l+k-1,n-1)tmp = rwhile l<r:ls[l],ls[r]=ls[r],ls[l]l+=1r-=1l = tmp+k+1return ''.join(ls)

2/1 81. 搜索旋转排序数组 II

找到当前旋转的位置 nums[n-1]>=nums[0]
恢复原来顺序 再进行二分查找

python">def search(nums, target):""":type nums: List[int]:type target: int:rtype: bool"""def find(nums):l,r = 0,len(nums)-1while l<=r:mid = l +(r-l)//2if nums[mid]==target:return Trueelif nums[mid]<target:l = mid+1else:r = mid-1return Falseif len(nums)==1:return nums[0]==targetif len(nums)==2:return nums[0]==target or nums[1]==targetif nums[0]<nums[-1]:return find(nums)for i in range(1,len(nums)):if nums[i]<nums[i-1]:return find(nums[i:]+nums[:i])return False

2/2 598. 区间加法 II

最大整数即为每次都加到的区间
遍历记录x,y的最小值

python">def maxCount(m, n, ops):""":type m: int:type n: int:type ops: List[List[int]]:rtype: int"""curx,cury=m,nfor x,y in ops:curx = min(curx,x)cury = min(cury,y)return curx*cury


http://www.ppmy.cn/embedded/160324.html

相关文章

第五天 初步了解ArkTS和ArkUI

初步了解ArkTS和ArkUI&#xff0c;可以从以下几个方面进行概述&#xff1a; 一、ArkTS简介 定义与关系&#xff1a; ArkTS是HarmonyOS&#xff08;鸿蒙系统&#xff09;优选的主力应用开发语言。它基于TypeScript&#xff08;TS&#xff09;进行扩展&#xff0c;兼容TS的所有特…

【Spring Boot】解锁高效安全之门:登录令牌技术的实战应用与价值解析

前言 &#x1f31f;&#x1f31f;本期讲解关于token令牌技术介绍~~~ &#x1f308;感兴趣的小伙伴看一看小编主页&#xff1a;GGBondlctrl-CSDN博客 &#x1f525; 你的点赞就是小编不断更新的最大动力 &#x1f386;那么废话不多说…

langchain教程-2.prompt

前言 该系列教程的代码: https://github.com/shar-pen/Langchain-MiniTutorial 我主要参考 langchain 官方教程, 有选择性的记录了一下学习内容 这是教程清单 1.初试langchain2.prompt3.OutputParser/输出解析4.model/vllm模型部署和langchain调用5.DocumentLoader/多种文档…

Java集合概述(Ⅱ)

基础 之前写过一个比较粗糙的版本&#xff1a;Java集合概述 本文章补充了Java集合体系当中的一些细节 Collection接口的方法 List接口&#xff0c;Set接口都会继承这些方法 List接口 特点&#xff1a;存取顺序有序&#xff08;因为有下标&#xff09;&#xff0c;可重复&a…

Web_php_unserialize

代码审计 <?php class Demo { private $file index.php;public function __construct($file) { $this->file $file; }、 //接收一个参数 $file 并赋值给私有属性 $filefunction __destruct() { echo highlight_file($this->file, true); } //在对象销毁时调用&…

制作PE启动盘(内含Win11 iso镜像)

前言 本文用于记录制作PE启动盘过程&#xff0c;学习记录用&#xff0c;如有不对请指出&#xff0c;谢谢&#xff01; 参考视频&#xff1a; 1. 微PE下载&#xff1a;https://www.bilibili.com/video/BV1vT4y1n7JX/?spm_id_from333.788.top_right_bar_window_history.conte…

基于springboot的体质测试数据分析及可视化设计

作者&#xff1a;学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等 文末获取“源码数据库万字文档PPT”&#xff0c;支持远程部署调试、运行安装。 项目包含&#xff1a; 完整源码数据库功能演示视频万字文档PPT 项目编码&#xff1…

3-kafka服务端之控制器

文章目录 概述控制器的选举与故障恢复控制器的选举故障恢复 优雅关闭分区leader的选举 概述 在Kafka集群中会有一个或多个broker&#xff0c;其中有一个broker会被选举为控制器&#xff08;Kafka Controler&#xff09;&#xff0c;它负责管理整个集群中所有分区和副本的状态。…