程序媛詹妮弗
终身学习

Sliding Window模板

    public int slidingWindowTemplate(String s, String t) {   
        Map<Character, Integer> map = new HashMap<>();
        int result = 0;
        int counter = map.size();// 用count来track当前的滑动窗口是否valid
        int start = 0, i = 0;  // [start...i]维护一个滑动窗口
        
        while(i < s.length()){
            char c1 = s.charAt(c1);
            if( map.containsKey(c1) ){
                map.put(c1, map.get(c1)+1);
                if(map.get(c) == 1) counter++;
            }
            i++;
        

            while(counter == 0){ //当前滑动窗口生态被打破,寻找下个有效的滑动窗口
                char c2 = s.charAt(start);
                if(map.containsKey(c2)){
                    map.put(c2, map.get(c2) - 1);//plus or minus one
                    if(map.get(tempc) == 0) counter--;
                }          
                start++;
            }

            //随时更新结果
            result = Math.max(result,  i - start);
        }
        return result;
    }

  

 

Sliding Window高频题:

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

public String minWindow(String s, String t) {      
        int[] mapT = new int[256];
        int[] mapS = new int[256];
        String result = "";
        if(s.length() < t.length()) return result;
        
        for(int i = 0; i < t.length(); i++){
            char c1 = t.charAt(i);
            mapT[c1] ++;
        }
        
        int count = 0; 
        int start = 0;
        for(int i = 0; i < s.length(); i++){
            char c2 = s.charAt(i);
            mapS[c2]++; 
            
            if(mapT[c2] >= mapS[c2]){
                count++;         
            }
            
            if(count == t.length()){
                while(mapS[s.charAt(start)] > mapT[s.charAt(start)]){
                    mapS[s.charAt(start)]--;
                    start++;
                }
                
                if( result == "" || i - start + 1 < result.length()){
                    result = s.substring(start, i+1);
                }
            }          
        }
        return result;
    }

  

 

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

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

 

public int lengthOfLongestSubstringTwoDistinct(String s) {
        int result = 0;
        int start = 0, i = 0;
        Map<Character, Integer> map = new HashMap<>();
        int count = 0;
        
        while(i < s.length()){
            char c = s.charAt(i);
            map.put(c, map.getOrDefault(c,0) +1);
            if(map.get(c) == 1) count++;
            i++;
          
            while(count > 2){
                char c2 = s.charAt(start);
                map.put(c2, map.get(c2) -1);
                
                if(map.get(c2) == 0){
                    count --;
                } 
                start++;
            } 
            result = Math.max(result,  i - start);    
        }
        return result;
    }

 

  

 

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

 

posted on 2020-12-29 03:15  程序媛詹妮弗  阅读(80)  评论(0编辑  收藏  举报