zoukankan      html  css  js  c++  java
  • 76. Minimum Window Substring 76.最小窗口子字符串

    Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

    Example:

    Input: S = "ADOBECODEBANC", T = "ABC"
    Output: "BANC"

    就是在模板的基础上,先得赋值给head。求出字符串长度
    if (end - begin < len) {
                        len = end - begin;
                        head = begin;
                    }
    
    

    再用一下substring就行了

    s.substring(head, head+len)
    public class Solution {
        public String minWindow(String s, String t) {
            List<Integer> result = new LinkedList<>();
            if(t.length()> s.length()) return "";
            Map<Character, Integer> map = new HashMap<>();
            
            for(char c : t.toCharArray()){
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            int counter = map.size();
            
            int begin = 0, end = 0;
            int head = 0;
            int len = Integer.MAX_VALUE;
            
            
            while(end < s.length()){
                char c = s.charAt(end);
                if( map.containsKey(c) ){
                    map.put(c, map.get(c)-1);
                    if(map.get(c) == 0) counter--;
                }
                end++;
                
                while(counter == 0){
                    char tempc = s.charAt(begin);
                    if(map.containsKey(tempc)){
                        map.put(tempc, map.get(tempc) + 1);
                        if(map.get(tempc) > 0){
                            counter++;
                        }
                    }
                    
                    if (end - begin < len) {
                        len = end - begin;
                        head = begin;
                    }
    
                    begin++;
                }
                
            }
            
            //cc
            if (len == Integer.MAX_VALUE) return "";
            return s.substring(head, head + len);
        }
    }
    View Code
  • 相关阅读:
    CAFFE安装(3):cuDNN v4
    监测查询性能(1)
    SQL Server 中的三种分页方式
    使用DBCC SHOW_STATISTICS展示索引的统计信息
    查询表的分配单元数据
    Node.js中的事件
    node-mysql中的连接池代码学习
    Excel动态生成JSON
    使用SignalR实现比特币价格实时刷新
    使用Async同步执行异步函数
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13068736.html
Copyright © 2011-2022 走看看