其他分享
首页 > 其他分享> > 题型总结之Sliding Window

题型总结之Sliding Window

作者:互联网

Sliding Window模板

int[]map = new int[256];  
int start = 0, i = 0;  // [start...i]维护一个滑动窗口
int count = 0;  //用count来track当前的滑动窗口是否valid
int result = 0;

while( i < s.length()){
    char c1= s.charAt(i);
    if(map[c1] >0) count++;  // 随题意变化。
    map[c1]++;
    i++;
    
    while(count > 0){  // 随题意变化。一旦count检测到当前滑动窗口invalid,要move指针start直至找到下一个valid sliding window
        char c2 = s.charAt(start);
        if(map[c2] > 1) count --;  // 随题意变化。
        map[c2] --;
        start ++;   
    }
    result = Math.max(result,  i - start);
}   

 

Sliding Window高频题:

[leetcode]76. Minimum Window Substring最小字符串窗口

[leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

[leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串

[leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串

 

标签:count,题型,map,int,++,start,Window,Sliding
来源: https://www.cnblogs.com/liuliu5151/p/14204559.html