有框架
设置头部和尾部
for(移动尾部直到最后){
更新窗口值
while(当窗口值刚符合条件或刚不符合条件){
移动头部直到符合条件或刚不符合条件
记录窗口值和结果
}
}
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
unordered_map<int,int> window;
int left = 0,right = 0;
int res = INT_MAX;
int cnt = 0;
while(right < nums.size()){
cnt += nums[right];
while(cnt >= target){
res = min(res,right - left + 1);
int tmp = nums[left];
left++;
cnt -= tmp;
}
right++;
}
return res == INT_MAX?0:res;
}
};
904. 水果成篮
这里与上面不同的地方在于,上面的过程是一开始不符合条件,头部一直动直到符合条件,是需要得到刚>=target的窗口,因此移动头部直到符合;下面是一开始就符合条件,只是需要得到最大符合条件的窗口,所以需要得到刚破坏条件的时候
因此导致了判断条件不同
class Solution {
public:
int totalFruit(vector<int>& fruits) {
unordered_map<int,int> window;
int need = 2;
int left = 0,right = 0;
int res = 0;
while(right < fruits.size()){
int tmp = fruits[right];
window[tmp]++;
while(window.size() > need){
int tmp = fruits[left];
left++;
window[tmp]--;
if(window[tmp] == 0)window.erase(tmp);
}
res = max(res,right - left + 1);
right++;
}
return res;
}
};
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char,int> window,need;
for(auto c:t)need[c]++;
int left = 0,right = 0;
int valid = 0;
int len = INT_MAX;
int start = 0;
while(right < s.size()){
char c = s[right];
if(need.find(c) != need.end()){
window[c]++;
if(window[c] == need[c]){
++valid;
}
}
while(valid == need.size()){
if(right - left + 1 < len){
start = left;
len = right - left + 1;
}
char d = s[left];
left++;
if(need.find(d) != need.end()){
if(window[d] == need[d]){
valid--;
}
window[d]--;
}
}
right++;
}
return len == INT_MAX?"":s.substr(start,len);
}
};
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map<char,int> need,window;
for(auto c:s1)need[c]++;
int left = 0,right = 0;
int valid = 0;
while(right < s2.size()){
char tmp = s2[right];
if(need.find(tmp) != need.end()){
window[tmp]++;
if(window[tmp] == need[tmp]){
valid++;
}
}
while(right - left + 1 >= s1.size()){
if(valid == need.size())return true;
char tmp = s2[left];
left++;
if(need.find(tmp) != need.end()){
if(window[tmp] == need[tmp]){
valid--;
}
window[tmp]--;
}
}
right++;
}
return false;
}
};
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
unordered_map<char,int> need,window;
for(auto c:p)need[c]++;
vector<int> res;
int left = 0,right = 0;
int valid = 0;
while(right < s.size()){
char tmp = s[right];
++right;
if(need.count(tmp)){
window[tmp]++;
if(window[tmp] == need[tmp])valid++;
}
while(right - left >= p.size()){
if(valid == need.size()){
res.push_back(left);
}
char c = s[left];
++left;
if(need.count(c)){
if(window[c] == need[c]){
valid--;
}
window[c]--;
}
}
}
return res;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> window;
int left = 0,right = 0;
int res = 0;
while(right < s.size()){
char c = s[right];
right++;
window[c]++;
while(window[c] > 1){
char d = s[left];
++left;
window[d]--;
}
res = max(res,right - left);
}
return res;
}
};