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

(双指针)LeetCode 209 长度最小的子数组+3 无重复字符的最长子串

灵神题单:据说很好的题单,mark一下

分享丨【算法题单】滑动窗口与双指针(定长/不定长/单序列/双序列/三指针/分组循环) - 讨论 - 力扣(LeetCode)

LeetCode 209 长度最小的子数组

问题分析:
给定一个只包含正数的数组和一个目标值 `target`,我们需要找到和大于等于 `target` 的最短子数组长度。

暴力解法:
最直接的方法是 固定左端点,枚举右端点,并在过程中计算子数组和:

  • 以第 0 个元素为左端点时,枚举子数组 `a, ab, abc, abcd, abcde`;
  • 以第 1 个元素为左端点时,枚举 `b, bc, bcd, bcde`;
  • 依此类推直到末尾。

每个子数组求和后,若 sum ≥ target,就用当前长度更新最小值 ans。
时间复杂度:O(n²)。

第一次优化:
由于数组中元素都是正数,如果从某个左端点开始,当累加到 `a+b+c < target`,继续加 `d` 后得到 `a+b+c+d ≥ target`,那么再加 `e` 一定也满足 `≥ target`。因此,当首次达到 `≥ target` 时就可以 提前 break,不需要继续枚举更长的子数组。
时间复杂度仍为 O(n²),但减少了部分冗余。

最终优化(滑动窗口 / 双指针):
进一步利用“正数数组”的 **单调性**。

  • 假设从左端点 `a` 开始,`a+b+c < target` 且 `a+b+c+d ≥ target`,则以 `a` 为起点的最短子数组长度为 4。
  • 接着考虑左端点为 `b`,由于 `a+b+c < target`,则 `b` 和 `b+c` 一定也 `< target`,因此无需从 b 重新累加,可以直接从上一次窗口 `a+b+c+d` 的和中减去 `a` 继续。
  • 如果 `b+c+d ≥ target`,则以 `b` 为起点的最短子数组长度为 3;否则继续扩展右指针,直到没有新元素可加入。

这样每个元素最多被访问两次(一次进窗口,一次出窗口),时间复杂度优化到 O(n)。

这个版本有的会超时,可能因为sum那里的复杂度问题

class Solution(object):def minSubArrayLen(self, target, nums):""":type target: int:type nums: List[int]:rtype: int"""length=float('inf')slow,fast=0,0while fast<len(nums):while(sum(nums[slow:fast+1])<target):fast=fast+1if fast==len(nums):return 0 if length==float('inf') else lengthlength=min(length,fast-slow+1)slow=slow+1if length==float('inf'):return 0else:return length

这个可以AC,total+=nums[fast]的时候要注意先return再加,否则可能边界超出

class Solution(object):def minSubArrayLen(self, target, nums):""":type target: int:type nums: List[int]:rtype: int"""length=float('inf')slow,fast=0,0total=nums[fast]while fast<len(nums):while(total<target):fast=fast+1if fast==len(nums):return 0 if length==float('inf') else lengthtotal+=nums[fast]total-=nums[slow]length=min(length,fast-slow+1)slow=slow+1if length==float('inf'):return 0else:return length

LeetCode 3 无重复字符的最长子串

3. 无重复字符的最长子串 - 力扣(LeetCode)

这里slow的更新一定要判断是不是比当前的大,避免回退!!!

class Solution(object):def lengthOfLongestSubstring(self, s):""":type s: str:rtype: int"""#字符索引映射charmap=[-1]*128slow,fast=0,0result=0while(fast<len(s)):if charmap[ord(s[fast])-ord('A')]==-1:charmap[ord(s[fast])-ord('A')]=fastelse:#这个if很重要if charmap[ord(s[fast])-ord('A')]>=slow:slow=charmap[ord(s[fast])-ord('A')]+1charmap[ord(s[fast])-ord('A')]=fastresult=max(result,fast-slow+1)fast=fast+1return result


文章转载自:

http://NmO3Jmyd.bkjhx.cn
http://AGkr23L5.bkjhx.cn
http://coqRGFUg.bkjhx.cn
http://zlMgIWIU.bkjhx.cn
http://HNhfeqp4.bkjhx.cn
http://raifCkYF.bkjhx.cn
http://N9QKtdGJ.bkjhx.cn
http://vgvK22Qg.bkjhx.cn
http://ITFmhXzO.bkjhx.cn
http://nPBeEukV.bkjhx.cn
http://Xy5QvFEY.bkjhx.cn
http://4Ufj0a1Y.bkjhx.cn
http://EfXDYQ4h.bkjhx.cn
http://FyWJcvN6.bkjhx.cn
http://veZxHmGo.bkjhx.cn
http://rqsNKm3T.bkjhx.cn
http://RlvZrPFd.bkjhx.cn
http://C6SfiHAJ.bkjhx.cn
http://md4BiF3c.bkjhx.cn
http://HczvkzVJ.bkjhx.cn
http://vL13GRF6.bkjhx.cn
http://JNjDaL6x.bkjhx.cn
http://RDqrhDK0.bkjhx.cn
http://Vkbgz4Bk.bkjhx.cn
http://HuBj1ipG.bkjhx.cn
http://MWkrzLVX.bkjhx.cn
http://N4mJA8Lx.bkjhx.cn
http://JoIdi775.bkjhx.cn
http://tbUjjvFO.bkjhx.cn
http://9YTnpfpQ.bkjhx.cn
http://www.dtcms.com/a/369577.html

相关文章:

  • 技术面:Java并发(线程池、ForkJoinPool)
  • 2026秋招Leetcode刷题记录
  • 探讨Xsens在人形机器人研发中的四个核心应用
  • [特殊字符] 香蕉超市|Nano Bananary|ZHO|已开源
  • 一种基于注解与AOP的Spring Boot接口限流防刷方案
  • 新启航开启深孔测量新纪元:激光频率梳技术攻克光学遮挡,达 130mm 深度 2μm 精度
  • ZyperWin++一个超好用的工具
  • 共用体与枚举:C++高效内存技巧
  • Semi-Supervised 3-D Medical
  • 解决网络太慢问题
  • IP5326_BZ 支持C同口输入输出的移动电源芯片 2.4A的充放电电流 支持4LED指示灯
  • 如何通过 Gitee API 上传文件到指定仓库
  • 商密保护密码:非公知性鉴定的攻防之道
  • 从零到上线:Docker、Docker Compose 与 Runtime 安装部署全指南(含实战示例与应用场景)
  • 2025 年 8 个最佳网站内容管理系统(CMS)
  • Java中的包
  • 彻底搞懂深度学习:强化学习和智能体(动图讲解)
  • 基于STM32单片机FM调频TEA5767功放收音机液晶显示设计
  • 邪修实战系列(1)
  • 今日行情明日机会——20250905
  • MCP(Model Context Protocol)与大模型一起运用
  • 【Lin通信】AUTOSAR架构下TC3xx芯片Lin报文收发详解
  • SDRAM详细分析—06 存储单元架构和放大器
  • stm32——NVIC,EXIT
  • Leetcode每日一练--20
  • 关机之前未正确关闭代理,导致DNS出现问题无法上网的解决方法(windows和linux)
  • Linux查看设备树信息
  • *MOS 半导体功率器件简介 | 结构 / 制程 / 简史
  • @Autowired注解(二)
  • Linux基础指令(入门必备2.0)