当前位置: 首页 > news >正文

【Hot100】LeetCode—76. 最小覆盖子串

题目

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

1- 思路

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


2- 实现

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

在这里插入图片描述

class Solution {public String minWindow(String s, String t) {// 定义两个 HashMapHashMap<Character,Integer> hs = new HashMap<>();HashMap<Character,Integer> ht = new HashMap<>();// 定义 int cnt = 0;String res = "";// 初始化 htfor(int i = 0 ; i < t.length();i++){char c = t.charAt(i);ht.put(c,ht.containsKey(c) ? ht.get(c)+1:1);}// 遍历 sfor(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<Character,Integer> ht = new HashMap<>();HashMap<Character,Integer> window = new HashMap<>();int cnt = 0;String res = "";// 2.遍历 t 初始化 htfor(int i = 0 ; i < t.length();i++){char c = t.charAt(i);ht.put(c,ht.containsKey(c)? ht.get(c)+1:1);}// 3.遍历 sfor(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);}// 更行 resif(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);}
}

http://www.lryc.cn/news/412888.html

相关文章:

  • 删除排序链表中的重复元素 II(LeetCode)
  • 【Java】解决如何将Http转为Https加密输出
  • 二叉树链式结构的实现(递归的暴力美学!!)
  • Python | Leetcode Python题解之第312题戳气球
  • 远程访问mysql数据库的正确打开方式
  • 网络6 -- udp_socket 实现 echo服务器
  • ASUS/华硕幻15 2020 冰刃4 GX502L GU502L系列 原厂win10系统 工厂文件 带F12 ASUS Recovery恢复
  • simulink绘制bode图
  • 知识工程视角下的软件研发
  • 深度学习------权重衰退
  • 【算法】退火算法 Simulated Annealing
  • 深入理解 Git `git add -p` 命令中的交互选项
  • HTML JavaScript 闪光涟漪
  • FastAPI之Depends
  • AttributeError: module ‘jwt‘ has no attribute ‘decode‘解决方案
  • C++——C++11
  • day12 多线程
  • DeferredResult 是如何实现异步处理请求的
  • VUE3——001(03)、开发环境配置(node.js/mvn/java/ngix/tomact/vue3)
  • TCP/IP_TCP协议
  • 鸿蒙应用框架开发【简单时钟】 UI框架
  • MySQL是如何实现数据排序的
  • 【测试架构师修炼之道】读书笔记
  • C++ Functor仿函数
  • 【EI会议征稿通知】第五届大数据、人工智能与软件工程国际研讨会(ICBASE 2024)
  • 微信小程序多端框架实现app内自动升级
  • C# Log4Net应用
  • pytest8.x版本 中文使用文档-------32.示例:使用自定义目录收集器
  • c语言第七天笔记
  • 软件测试经理工作日常随记【8】-UI自动化_加密接口的传输