【Hot100】LeetCode—76. 最小覆盖子串
创始人
2024-11-15 15:34:41

题目

  • 原题链接:76. 最小覆盖子串

1- 思路

利用两个哈希表解决分为 :① 初始化哈希表②遍历 s,处理当前元素,判断当前字符是否有效③收缩窗口④更新最小覆盖子串


2- 实现

⭐76. 最小覆盖子串——题解思路

在这里插入图片描述

class Solution {     public String minWindow(String s, String t) {          // 定义两个 HashMap         HashMap hs = new HashMap<>();         HashMap ht = new HashMap<>();         // 定义          int cnt = 0;         String res = "";          // 初始化 ht         for(int i = 0 ; i < t.length();i++){             char c = t.charAt(i);             ht.put(c,ht.containsKey(c) ? ht.get(c)+1:1);          }          // 遍历 s         for(int i = 0, j = 0 ; i < s.length();i++){             char c = s.charAt(i);             hs.put(c, hs.containsKey(c) ? hs.get(c)+1 : 1);              // 判断 i 合法             if(ht.containsKey(c) && hs.get(c) <= ht.get(c)) cnt++;              // 缩小区间             while (j <= i && (!ht.containsKey(s.charAt(j)) || hs.get(s.charAt(j)) > ht.get(s.charAt(j))))              {                 hs.put(s.charAt(j), hs.get(s.charAt(j ++)) - 1);             }              // 3 收集结果             // 首先是必须等于 cnt && (hs.length()> (i-j+1) || res.length()<1)             if(cnt==t.length() && ( res.length() > (i-j+1) || res.length()<1)){                 res = s.substring(j,i+1);             }          }         return res;     } } 

3- ACM 实现

public class minWindow {      public static String minWindow(String s,String t){         // 1.数据结构         HashMap ht = new HashMap<>();         HashMap window = new HashMap<>();         int cnt = 0;         String res = "";          // 2.遍历 t 初始化 ht         for(int i = 0 ; i < t.length();i++){             char c = t.charAt(i);             ht.put(c,ht.containsKey(c)? ht.get(c)+1:1);         }          // 3.遍历 s         for(int i = 0,j=0 ; i < s.length();i++){             char cc = s.charAt(i);             window.put(cc,window.containsKey(cc)? window.get(cc)+1:1);              // 判 cc 断有效性             // 在 ht 中             if(ht.containsKey(cc) && window.get(cc) <=                     ht.get(cc)) cnt++;              // 窗口收缩             while(j<=i && (!ht.containsKey(s.charAt(j)) || window.get(s.charAt(j)) > ht.get(s.charAt(j)))){                 window.put(s.charAt(j),window.get(s.charAt(j++))-1);             }              // 更行 res             if(cnt == t.length() && (res.length()>(i-j+1) || res.length()<1)){                 res = s.substring(j,i+1);             }         }         return res;     }       public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         System.out.println("输入字符串1");         String s = sc.nextLine();          System.out.println("输入字符串2");         String t = sc.nextLine();         String res = minWindow(s,t);         System.out.println("结果是"+ res);     } }  

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...