forked from gongluck/CVIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76.最小覆盖子串.cpp
72 lines (63 loc) · 1.72 KB
/
76.最小覆盖子串.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem76.h"
using namespace std;
// @before-stub-for-debug-end
/*
* @lc app=leetcode.cn id=76 lang=cpp
*
* [76] 最小覆盖子串
*/
// @lc code=start
class Solution
{
public:
string minWindow(string s, string t)
{
std::unordered_map<char, int> need, window;
for (const auto &c : t)
{
++need[c];
}
int left = 0;
int right = 0;
int matchs = 0;
int rstart = 0;
int rlen = INT_MAX;
for (; right < s.size(); ++right)
{
//need.count(s[right]) <= 0 时不需要加入窗口,相当于直接continue
//need.count(s[right]) <= 0 时 matchs == need.size() 必为 false
if (need.count(s[right]) > 0 && ++window[s[right]] == need[s[right]])
{
//边缘触发
++matchs;
}
bool updata = false;
//need.count(s[right]) <= 0 时 matchs == need.size() 必为 false
while (matchs == need.size())
{
//结果需要更新
updata = true;
if (need.count(s[left]) > 0 && window[s[left]]-- == need[s[left]])
{
//边缘触发
--matchs;
}
++left;
}
if (updata)
{
auto len = right - (left - 1) + 1;
if (len < rlen)
{
rlen = len;
rstart = left - 1;
}
}
}
return rlen == INT_MAX ? "" : s.substr(rstart, rlen);
}
};
// @lc code=end