和为K的子数组(560)

先看代码

class Solution {
public int subarraySum(int[] nums, int k) {
int res = 0;
int preSum = 0;
Map<Integer, Integer> cnt = new HashMap<>(nums.length);
for (int num : nums){
cnt.merge(preSum, 1, Integer::sum);
preSum += num;
res += cnt.getOrDefault(**preSum - k**, 0);
}
return res;
}
}
  • 分析

cnt用来记录相同前缀和的前缀数量

一次循环一边更新相同前缀和的前缀数量, 一边将吻合的数据放入res

preSum - k 是前缀和的关键

  • 感悟

只有”单调”的数据集才适合用滑动窗口, 非”单调”的前缀和才好做 , 否则每次移动窗口都带来混乱

“单调”指的是 任意 num>0且单调递增 OR 任意 num < 0 且单调递减

不符合单调

符合单调

滑动窗口最大值(560)

先看代码

class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] res = new int[n-k+1];
Deque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < n; i++){
while (!queue.isEmpty() && nums[queue.getLast()] <= nums[i]){
queue.removeLast();
}
queue.addLast(i); if (i-k >= queue.getFirst()){
queue.removeFirst();
}
if(i-k+1 >= 0) res[i-k+1] = nums[queue.getFirst()];
}
return res;
}
}
  • 分析

通过维护单调递减队列queue

队列中存储数据下标

  • 感悟

很多题目都是通过存储下标 因为一个下标含有两个有效数据 → <于数据集的位置, 数据的值>

最小覆盖字串(076)

先看代码

class Solution {
public String minWindow(String s, String t) {
int needCnt = 0;
int[] need = new int[128];
for (char c : t.toCharArray()){
if (need[c] == 0){
needCnt++;
}need[c]++;
}
int n = s.length();
int resLef = -1;
int resRig = n; int lef = 0;
for (int rig = 0; rig < n; rig++){
char cR = s.charAt(rig);
need[cR]--;
if (need[cR] == 0){
needCnt--;
}
while (needCnt == 0){
if(resRig-resLef > rig-lef){
resLef = lef;
resRig = rig;
}
char cL = s.charAt(lef);
if (need[cL] == 0) needCnt++;
need[cL]++;
lef++;
}
} return resLef < 0 ? "" : s.substring(resLef, resRig+1);
}
}
  • 分析

通过滑动窗口遍历

通过数组need存储需要各种类字符数量, needCnt 存储需要的字符种类

  • 感悟

施工中…

hot100之子串的更多相关文章

  1. leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口

    可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...

  2. LeetCode[5] 最长的回文子串

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. C语言计算字符串子串出现的次数

    #include<stdio.h>#include<string.h>int substring(char *str,char *str1);//函数原型int main(vo ...

  5. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  6. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. mysql判断一个字符串是否包含某子串

    使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0 例子:判断site表中的url是否包含'http://'子串,如果不包含则拼接在url字符串开头 update ...

  10. 最长回文子串(Longest Palindromic Substring)

    这算是一道经典的题目了,最长回文子串问题是在一个字符串中求得满足回文子串条件的最长的那一个.常见的解题方法有三种: (1)暴力枚举法,以每个元素为中心同时向左和向右出发,复杂度O(n^2): (2)动 ...

随机推荐

  1. [Winform]在Form里显示模态对话框ModalDialog

    问题 如何在WinForm的一个Form里面弹出一个模态Dialog? 背景 程序的框架是Winform,只有一个窗口MainForm.MainForm里面是一个TabControl,每个TabPag ...

  2. Delphi 时间控制窗口标题栏文字或任务栏标题文字滚动

    1.定义一个全局变量保存显示到标题栏的字符串,strScroll: Widestring = '风行天下 - By WindSon '; 2.添加一个Timer控件,设置属性Interval := 3 ...

  3. 【SpringMVC】映射请求参数 & 请求头

    映射请求参数 & 请求参数 请求处理方法签名 Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中. Spring MVC 对控制器处理方法签名的限制 ...

  4. 【QT】使用Qxlsx读取Excel单元格中函数表达式的结果值

    [QT]使用Qxlsx读取Excel单元格中函数表达式的结果值 零.起因 是这样的,目前朋友托我写一款模板生成软件,任务是先把他写的程序文件复制一份出来,然后再根据Excel中对应位置的单元格的值,修 ...

  5. C#/.NET/.NET Core技术前沿周刊 | 第 33 期(2025年4.1-4.6)

    前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...

  6. kettle介绍-参数变量

    ETL中为什么使用参数变量 实现ETL的复用 D,Q,P环境不同,使用变量方便发布 有的条件需要外部传入 增量ETL 灵活性强 kettle中参数变量种类 Environment Variables ...

  7. UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe8 in position...解决方法

    运行python程序,出现了以下错误: File "C:/��͸/python ѧϰ/god_mellonѧϰpython/untitled2/fofa_py2.py", line ...

  8. 工具 | StarCodeSecurity

    0x00 简介 StarCodeSecurity是一款图形化的代码审计工具. 下载地址: StarCodeSecurity下载:StarCodeSecurity下载 0x01 功能说明 支持对规则进行 ...

  9. docker部署SonarQube流程及相关问题汇总

    环境说明: sonarqube版本:10.4.1-community PostgreSql版本:14.1 系统环境:centos7.6(x86_64) 部署流程 1.PostgreSql的安装部署 在 ...

  10. kubernetes理论

    kubernetes特性:自动装箱,自我修复,自动水平扩展,自动服务发现,服务自动负载均衡,自动发布和回滚 秘钥和配置管理,存储编排,批量处理执行 kubernetes架构:master/node m ...