hot100之子串
和为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之子串的更多相关文章
- leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口
可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- C语言计算字符串子串出现的次数
#include<stdio.h>#include<string.h>int substring(char *str,char *str1);//函数原型int main(vo ...
- [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 ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [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 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- mysql判断一个字符串是否包含某子串
使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0 例子:判断site表中的url是否包含'http://'子串,如果不包含则拼接在url字符串开头 update ...
- 最长回文子串(Longest Palindromic Substring)
这算是一道经典的题目了,最长回文子串问题是在一个字符串中求得满足回文子串条件的最长的那一个.常见的解题方法有三种: (1)暴力枚举法,以每个元素为中心同时向左和向右出发,复杂度O(n^2): (2)动 ...
随机推荐
- 14 个 Linux 下 CPU 监控工具
01. top top是最常用的查看系统资源使用情况的工具,包括CPU.内存等等资源. 这里主要关注CPU资源. 1.1 /proc/loadavg load average取自/proc/loada ...
- Gentoo 使用 Obs进行屏幕分享
Obs-Studio 安装使用 on Hyprland 依赖安装 pipewire wireplumber xdg-desktop-portal-hyprland emerge -aq pipewir ...
- nrm
nrm npm install -g nrm nrm ls nrm use taobao Tips:不要使用cnpm,会有些奇怪的问题,导致npm install失败. 参考
- pdf和图片的处理一记
python是非常好用的一门语言,因为它有很多别人封装好底层逻辑的库,我们只需要用简单的函数调用或者经过并不复杂的初始化过程即可.最近要投递简历,所以在处理简历的时候遇到一个问题,就是生成了一份pdf ...
- 如何使用Streamlit快速创建仪表盘?
上文有快速带大家了解streamlit,因为工作需要,这两天尝试构建了仪表盘,也就是咱们常说的Dashboard,本篇文章将教你如何使用 Streamlit 快速创建一个简单的仪表盘. 前言 Stre ...
- 记一次SQL隐式转换导致精度丢失问题的排查 → 不规范就踩坑
开心一刻 刚毕业的侄子给我发消息侄子:叔,人生太难了我:怎么呢?侄子:工作太难了,感情也太难了,怎么什么都这么难我:你还小啊侄子:大了就不难了?我:大了你就习惯了 问题复现 先准备表:数据源( tbl ...
- 【SQL周周练】给你无酸纸、变色油墨,你能伪造多少美金?
大家好,我是"蒋点数分",多年以来一直从事数据分析工作.从今天开始,与大家持续分享关于数据分析的学习内容. 本文是第 2 篇,也是[SQL 周周练]系列的第 2 篇.该系列是挑选或 ...
- 【HUST】网安|操作系统实验|实验四 设备管理、文件管理
文章目录 任务 任务1 编写一个Linux内核模块,并完成安装/卸载等操作. 1. 提示 2. 任务代码 3. 结果及说明 任务2 编写Linux驱动程序并编程应用程序测试. 1. 提示 2. 任务代 ...
- 结点的"最早开始和最晚开始和最早完成和最晚完成"
最早:方块表示 最晚:三角形表示 最早开始:2 最晚开始:15-5=10 最早完成:2+5=7 最晚完成:15 案例1 案例2
- 手把手教你实现MVVM架构
.markdown-body { color: rgba(89, 89, 89, 1); font-size: 15px; font-family: -apple-system, system-ui, ...