当前位置:网站首页>Leetcode-76- minimum covering substring
Leetcode-76- minimum covering substring
2022-06-11 21:27:00 【z754916067】
subject

Ideas
- shock , It was sliding window again
- Will the required letters exist hashMap Inside , Maintain with sliding window , See the code , Too lazy to write. .
Code
class Solution {
public String minWindow(String s, String t) {
// take t Chinese character addition hash surface
LinkedHashMap<Character,Integer> m1 = new LinkedHashMap();
for(int i=0;i<t.length();i++){
// Judge first whether there is value
if(m1.containsKey(t.charAt(i))){
m1.put(t.charAt(i),1+m1.get(t.charAt(i)));
}else m1.put(t.charAt(i),1);
}
// How many characters are needed in total
int needCount = t.length();
// Start in s Slide up the window use LR maintain At first L=0 R=0
int L=0,R=0;
int ansL=0,ansR=0;
while (L<=s.length()){
while(needCount!=0){
//R Slide right
if(R>=s.length()) break;
// Record the current R The letter of
char c = s.charAt(R);
if(m1.containsKey(c)){
// Get current value value If less than or equal to 0 Explanation is not necessary If it is greater than 0 explain needcount Sure --
int value = m1.get(c);
if(value>0) needCount--;
m1.put(c,value-1);
}else m1.put(c,-1); // Otherwise it is absolutely not important
R++;
}
// If you go here, it means that the current string already contains all You can begin to L Slide right to eliminate unnecessary elements
while(true){
// The premise to jump out is to encounter letters that cannot be deleted I met +1 Will be greater than 0 The elements of Just jump out
// If you come here needCount Greater than 0 explain R Has come to the end You can no longer perform the following operations
if(needCount>0) break;
char c = s.charAt(L);
//m1 This must include c
int value = m1.get(c);
if(value+1>0) break;
else {
L++;
m1.put(c,value+1);
}
}
if(needCount>0) break;
// It means that the sliding window has been found Record length
if((ansL==0 && ansR==0 )||(ansR-ansL>R-L)){
ansL = L;
ansR = R;
}
// Ready for the next cycle Give Way L Move right
char c = s.charAt(L);
int value = m1.get(c);
L++;
m1.put(c,value+1);
needCount++;
}
return s.substring(ansL,ansR);
}
}
边栏推荐
- JVM|运行时数据区;程序计数器(PC寄存器);
- 字符串复制函数
- Online excel file parsing and conversion to JSON format
- Goland中在文件模板中为go文件添加个人声明
- 焕新升级 | 创新,从云商店开始
- Add personal statement for go file in file template in Golan
- Bipartite King
- Supplementary questions for the training ground on September 11, 2021
- Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
- 如何使用 SAP Kyma 控制台手动发送 SAP Commerce Cloud Mock 应用暴露的事件
猜你喜欢

LeetCode-110-平衡二叉树

How to Load Data from CSV (Data Preparation Part)

解决 img 5px 间距的问题

flutter系列之:flutter中常用的container layout详解

How to manually drag nodes in the Obsidian relationship graph

使用 float 创建一个网页页眉、页脚、左边的内容和主要内容。

Apache local multi port configuration

Only 38 years old! Zhou Chuan, a researcher of the Chinese Academy of Sciences, died unfortunately. Rao Yi sent a document to mourn: he guided me when he was still my student

Software test plan

LabVIEW Arduino电子称重系统(项目篇—1)
随机推荐
[advanced C language] integer storage in memory
LeetCode-98-验证二叉搜索树
LeetCode-76-最小覆盖子串
A problem of setting the private library of golang
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Bug -- coredump usage
Test plans and test cases
JVM runtime constant pool and direct memory
RANSAC extract cylinder (matlab built-in function)
JVM heap
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
Go language for loop
焕新升级 | 创新,从云商店开始
JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
[game theory complete information static game] strategic game
BZOJ3189 : [Coci2011] Slika
BZOJ3189 : [Coci2011] Slika
JVM object allocation policy TLAB
Comprehensive RTL code design method and precautions
How to Load Data from CSV (Data Preparation Part)