当前位置:网站首页>Niuke network: minimum coverage substring
Niuke network: minimum coverage substring
2022-06-22 19:03:00 【lsgoose】


This is the idea :
We maintain a speed pointer slow and fast, And a hash table
The hash table is used to record in S[slow,fast] Whether or not there is... In this string interval T In the character , If so, then >=0, At the beginning, it is initialized to -1. After that we will only update and T String related elements , It can be used unordered_map A function of count, If the element we are looking for is in it, we return 1, If not, return 0
Then let's maintain this interval , We make use of fast To traverse the entire string S:
1. Take out S[fast], If this element is T The elements in , Then the hash table record is incremented by one
2. If it is in the current maintenance interval [slow,fast] In the T All characters in , The length of the shortest string is updated , And update the interval to be returned [left, right] The value of is [slow, fast]
After the update , We try to reduce the size of the interval , About to take out the left element , take slow Add 1, Update hash table , Continue with the first judgment in step 2
The code is as follows :
class Solution {
public:
/**
*
* @param S string character string
* @param T string character string
* @return string character string
*/
bool check(unordered_map<char, int> &hash){
for(auto iter=hash.begin();iter!=hash.end();++iter){
if(iter->second<0){
return false;
}
}
return true;
}
string minWindow(string S, string T) {
int cnt=S.length()+1;
unordered_map<char, int> hash;
for(int i=0;i<T.length();++i){
hash[T[i]] -= 1;
}
int slow=0,fast=0;
int left=-1,right=-1;
for(;fast<S.length();++fast){
char c=S[fast];
if(hash.count(c)){
hash[c]++;
}
while(check(hash)){
// If the length is less than the minimum value of the current record
// Update interval value
if(cnt > fast-slow+1){
cnt=fast-slow+1;
left=slow;
right=fast;
}
// As long as it is satisfied to appear t Conditions for all characters of
// Just try to narrow the left range
char c=S[slow];
if(hash.count(c)){
hash[c]--;
}
slow++;
}
}
if(left==-1){
return "";
}
return string(S.begin()+left, S.begin()+right+1);
}
};边栏推荐
- 第八届 GopherChina 大会蓄势待发!
- Jenkins容器安装ruby-runtime插件失败报错解决
- How does flynk MySQL CDC guarantee the server_ Is the ID globally unique?
- Preliminary controller input of oculus learning notes (1)
- @“齐鲁多娇”幸运用户,山东5A景区喊你免费游园啦!
- [OWT] OWT client native P2P E2E test vs2017 build
- Filebeat收集日志数据传输到Redis,通过Logstash来根据日志字段创建不同的ES索引
- 新人报道的笔记
- Golang implements redis (10): local atomic transactions
- Some preliminary explorations of avoiding obstacles and finding paths by rays in unity
猜你喜欢

Grafana 9 正式发布,更易用,更酷炫了!

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比之sqlserver

wpa_supplicant的状态机迁移

Zero basic programming / reverse learning / over detection (Frida practice)

RSPS2022 Finalist | Dr. Yang Bai 简介

2022焊工(初级)特种作业证考试题库模拟考试平台操作

Beijing restorer's half moon: how to rekindle the fireworks in store management

Typescript (7) generic

A course for New Oriental transformation bilingual live broadcast to bring goods to the project manager

When do project managers particularly want to escape from work?
随机推荐
When do project managers particularly want to escape from work?
Concepts and solutions of redis' cache penetration, cache avalanche and cache breakdown problems
[win11] right click fix to modify the registry but not create a new one
第四届青年生命科学论坛 | 第一轮通知
Postman learning
The world's first AR contact lens, the entrance of metauniverse is really opened this time?
面试MySQL
牛客网:最小覆盖子串
Grafana 9 is officially released, which is easier to use and more cool!
每天5分钟玩转Kubernetes | Dashboard典型使用场景
Grafana 9 正式发布,更易用,更酷炫了!
Mysql如何删除数据库表中的某一列
不断重修的计划与变化
2022 G2 power plant boiler stoker question bank and online simulation examination
In May, 2022, China's game manufacturers and applications went to sea, with top 30 revenue in EMEA region
List的同步类比较
Alibaba cloud cannot find the account security group id problem during the account transfer
2022年G2电站锅炉司炉题库及在线模拟考试
Power BI的五个实用小技巧(文末赠书)
Pytoch -- error reporting solution: "torch/optim/adamw.py" beta1, unboundlocalerror: local variable 'beta1‘