博客
关于我
76. 最小覆盖子串
阅读量:288 次
发布时间:2019-03-03

本文共 1022 字,大约阅读时间需要 3 分钟。

在这里插入图片描述

在这里插入图片描述

class Solution:    def minWindow(self, s: str, t: str) -> str:        def dic_equal(dic1,dic2):            for key in dic1:                if dic2[key] < dic1[key]:                    return False            return True        t_dic = {   }        for t1 in t:            if t1 not in t_dic:                t_dic[t1] = 1            else:                t_dic[t1] = t_dic[t1] + 1                tmp_dic = {   }        for key in t_dic.keys():            tmp_dic[key] = 0        start = 0        res_start = -1        ans = len(s)        for i in range(len(s)):            if s[i] in t_dic:                tmp_dic[s[i]] = tmp_dic[s[i]] + 1            while dic_equal(t_dic, tmp_dic):                if i-start+1 <= ans:                    ans = i-start+1                    res_start = start                if s[start] in tmp_dic:                    tmp_dic[s[start]] = tmp_dic[s[start]] - 1                start = start + 1        if res_start == -1:            return ""        return s[res_start:res_start+ans]

滑动窗口思想非常经典

转载地址:http://ubsl.baihongyu.com/

你可能感兴趣的文章
mysql安全模式: sql_safe_updates
查看>>
mysql安装,卸载,连接
查看>>
MySQL安装之没有配置向导
查看>>
mysql安装出现 conflicts with mysql*的解决办法
查看>>
mysql安装卡在最后一步解决方案(附带万能安装方案)
查看>>
mysql安装和启动命令小结
查看>>
Mysql安装教程(命令行)
查看>>
mysql安装版安装
查看>>
MySQL安装配置教程(非常详细),从零基础入门到精通,看完这一篇就够了
查看>>
mysql安装配置简介
查看>>
MySQL定义和变量赋值
查看>>
mysql定时任务事件清理单表数据
查看>>
MySQL定时器Events
查看>>
mysql实战01|基础架构:一条SQL查询语句是如何执行的?
查看>>
Mysql实战之数据备份
查看>>
MySQL实战教程:从小白到大神的进阶之路!
查看>>
mysql实现成绩排名
查看>>
Mysql客户端中文乱码问题解决
查看>>
mysql客户端工具使用
查看>>
MySQL密码忘记,怎么办?
查看>>