zoukankan      html  css  js  c++  java
  • LeetCode 76. Minimum Window Substring

    题目

    从第一个字符串中找到最小的子串,让子串中包含第二个字符串中的每一个字符。

    我的思路来自滑动窗口思想,之前用来做自动摘要的。

    把第一个字符串中的在第二个字符串中出现的字符都标记出来,找到第一个符合条件的区间。也就是第一个窗口。

    然后依照之前的标记下标,开始滑动,区间左边每次进一格,右边一直寻找到满足条件的下边,一次滑动的过程就结束了。

    在这个过程中寻找最小值。

    一遍过。

    class Solution {
    public:
        map<int,int> m;
        map<int,int> m2;
        map<int,int> m3;
        int pos[100005];
        string minWindow(string s, string t) {
    
            int count=0;
    
            for(int i=0;i<t.length();i++)
            {
                if(m[t[i]]==0)
                    count++;
                m[t[i]]++;
            }
    
    
            int start=-1;
            int end=-1;
            int tag=0;
            for(int i=0;i<s.length();i++)
            {
                if(m[s[i]]!=0)
                {
                    m2[s[i]]++;
                    pos[tag++]=i;
                    if(start==-1)
                        start = tag-1;
    
                    if(m2[s[i]]==m[s[i]]&&m3[s[i]]==0&&count>0)
                    {
                        m3[s[i]]=1;
                        count--;
                    }
    
                    if(end==-1&&count==0)
                    {
                        end=tag-1;
                    }
                }
            }
    
            if(start==-1||end==-1)
                return "";
    
            int x = start;
            int y = end;
            int ansx = pos[x];
            int ansy = pos[y];
            int ans = pos[y]-pos[x]+1;
    
            m2.clear();
    
            for(int i=x;i<=y;i++)
            {
                m2[s[pos[i]]]++;
            }
    
            for(int i=1;i<tag;i++)
            {
                m2[s[pos[x]]]--;
                if(m2[s[pos[x]]]>=m[s[pos[x]]])
                {
                    x=i;
    
                    if(ans > pos[y]-pos[x]+1)
                    {
                        ansx=pos[x];
                        ansy=pos[y];
                        ans = pos[y]-pos[x]+1;
                    }
    
                }
                else
                {
                    int tag2=0;
                    for(int j=y+1;j<tag;j++)
                    {
                        m2[s[pos[j]]]++;
                        if(s[pos[j]]==s[pos[x]])
                        {
                            if(ans > pos[j] - pos[i] +1)
                            {
                                ansx=pos[i];
                                ansy=pos[j];
                                ans = pos[j]-pos[i]+1;
                            }
    
                            x=i;
                            y=j;
                            tag2=1;
                            break;
                        }
                    }
    
                    if(tag2==0)
                        break;
                }
            }
    
            string res="";
            for(int i=ansx;i<=ansy;i++)
            {
                res+=s[i];
            }
    
            return res;
    
        }
        
    };
    
  • 相关阅读:
    JZOJ 3845. 简单题(simple)
    JZOJ 3844. 统计损失(count)
    JZOJ 3843. 寻找羔羊(agnus)
    JZOJ 3833. 平坦的折线
    JZOJ 1956. 矩形
    JZOJ 3832. 在哪里建酿酒厂
    mysql 语法一 :case when详解
    阿里云推荐码
    redis配置文件详解(转)
    压力测试工具 webbench总结
  • 原文地址:https://www.cnblogs.com/dacc123/p/11766206.html
Copyright © 2011-2022 走看看