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

建设网站赚钱的方法百度云资源搜索入口

建设网站赚钱的方法,百度云资源搜索入口,网站蓝色和红色搭配,nodejs做网站leetcode原题如下: 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。 注意: 对于 t 中重复字符,我们寻找的子字符串中该字符数量…

leetcode原题如下:

        

        给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:

  • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
  • 如果 s 中存在这样的子串,我们保证它是唯一的答案。

        

解题思路---滑动窗口

如何判断当前的窗口包含所有 t 所需的字符呢?我们可以用一个哈希表表示 t 中所有的字符以及它们的个数,用一个哈希表动态维护窗口中所有的字符以及它们的个数,如果这个动态表中包含 t 的哈希表中的所有字符,并且对应的个数都不小于 t 的哈希表中各个字符的个数,那么当前的窗口是「可行」的。

官方题解:

    Map<Character, Integer> ori = new HashMap<Character, Integer>();Map<Character, Integer> cnt = new HashMap<Character, Integer>();public String minWindow(String s, String t) {Map<Character, Integer> ori = new HashMap<>();Map<Character, Integer> cnt = new HashMap<>();// 预处理,初始化 ori 表for (int i = 0; i < t.length(); i++) {char c = t.charAt(i);ori.put(c, ori.getOrDefault(c, 0) + 1);}int l = 0, r = 0;int len = Integer.MAX_VALUE, ansL = -1, ansR = -1;int sLen = s.length();int required = ori.size();  // 需要匹配的字符种类数量int formed = 0;  // 已经匹配的字符种类数量while (r < sLen) {char c = s.charAt(r);cnt.put(c, cnt.getOrDefault(c, 0) + 1);if (ori.containsKey(c) && cnt.get(c).equals(ori.get(c))) {formed++;}while (formed == required && l <= r) {if (r - l + 1 < len) {len = r - l + 1;ansL = l;ansR = l + len;}char leftChar = s.charAt(l);cnt.put(leftChar, cnt.get(leftChar) - 1);if (ori.containsKey(leftChar) && cnt.get(leftChar) < ori.get(leftChar)) {formed--;}l++;}r++;}return ansL == -1 ? "" : s.substring(ansL, ansR);}

注意:这里 t 中可能出现重复的字符,所以我们要记录字符的个数。

考虑如何优化? 如果 s=XX⋯XABCXXXX,t=ABC,那么显然 [XX⋯XABC]是第一个得到的「可行」区间,得到这个可行区间后,我们按照「收缩」窗口的原则更新左边界,得到最小区间。我们其实做了一些无用的操作,就是更新右边界的时候「延伸」进了很多无用的 X,更新左边界的时候「收缩」扔掉了这些无用的 X,做了这么多无用的操作,只是为了得到短短的 ABC。没错,其实在 s 中,有的字符我们是不关心的,我们只关心 t中出现的字符,我们可不可以先预处理 s,扔掉那些 t 中没有出现的字符,然后再做滑动窗口呢?也许你会说,这样可能出现 XXABXXC的情况,在统计长度的时候可以扔掉前两个 X,但是不扔掉中间的 X,怎样解决这个问题呢?优化后的时空复杂度又是多少?代码给出优化的版本.

public class MinimumWindowSubstring {public String minWindow(String s, String t) {// 用于记录 t 中各字符需要匹配的次数Map<Character, Integer> ori = new HashMap<>();// 用于记录当前窗口中各字符已匹配的次数Map<Character, Integer> cnt = new HashMap<>();// 预处理,初始化 ori 表for (int i = 0; i < t.length(); i++) {char c = t.charAt(i);ori.put(c, ori.getOrDefault(c, 0) + 1);}int l = 0, r = 0; // 窗口的左右边界int len = Integer.MAX_VALUE; // 记录当前最小窗口的长度int ansL = -1, ansR = -1; // 记录当前最小窗口的左右边界int sLen = s.length(); // 字符串 s 的长度int required = ori.size(); // 需要匹配的字符种类数量int formed = 0; // 已经匹配的字符种类数量// 右指针滑动while (r < sLen) {char c = s.charAt(r);cnt.put(c, cnt.getOrDefault(c, 0) + 1);// 如果当前字符在 ori 表中,并且已匹配次数等于 ori 表中的次数,增加已匹配字符种类数量if (ori.containsKey(c) && cnt.get(c).equals(ori.get(c))) {formed++;}// 左指针滑动,窗口收缩while (formed == required && l <= r) {// 更新最小窗口的长度和边界if (r - l + 1 < len) {len = r - l + 1;ansL = l;ansR = l + len;}// 移动左指针,减少已匹配字符的次数char leftChar = s.charAt(l);cnt.put(leftChar, cnt.get(leftChar) - 1);// 如果左边界字符在 ori 表中,并且减少后的次数小于 ori 表中的次数,减少已匹配字符种类数量if (ori.containsKey(leftChar) && cnt.get(leftChar) < ori.get(leftChar)) {formed--;}l++;}// 右指针继续滑动r++;}// 返回最小窗口对应的子串return ansL == -1 ? "" : s.substring(ansL, ansR);}
}

这段代码中的核心思想是使用滑动窗口来找到包含字符串 t 中所有字符的最小窗口。下面是代码中各部分的解释:

  1. oricnt 的初始化:

    • ori 表用于记录字符串 t 中各字符需要匹配的次数。
    • cnt 表用于记录当前窗口中各字符已匹配的次数。
  2. 预处理,初始化 ori 表:

    • 遍历字符串 t,将其中各字符及其需要匹配的次数记录在 ori 表中。
  3. 窗口的左右边界 lr

    • 使用两个指针 lr 来确定窗口。
  4. requiredformed

    • required 记录需要匹配的字符种类数量,即 ori 表的大小。
    • formed 记录已经匹配的字符种类数量,初始为 0。
  5. 右指针滑动(while (r < sLen)):

    • 不断移动右指针 r,直到窗口包含了字符串 s 中的所有字符。
  6. 左指针滑动,窗口收缩(while (formed == required && l <= r)):

    • 移动左指针 l,尝试缩小窗口的大小。
    • 更新最小窗口的长度和边界。
  7. 左右指针继续滑动,直至右指针到达字符串末尾:


文章转载自:
http://microsphere.tsnq.cn
http://unfamiliar.tsnq.cn
http://concentrated.tsnq.cn
http://coenacle.tsnq.cn
http://undauntable.tsnq.cn
http://gangmaster.tsnq.cn
http://dapping.tsnq.cn
http://fitfully.tsnq.cn
http://accountantship.tsnq.cn
http://afterbeat.tsnq.cn
http://underutilize.tsnq.cn
http://pigmentation.tsnq.cn
http://spindrift.tsnq.cn
http://prejudicial.tsnq.cn
http://apostasy.tsnq.cn
http://laevorotatory.tsnq.cn
http://diffusionist.tsnq.cn
http://strainmeter.tsnq.cn
http://clavier.tsnq.cn
http://betray.tsnq.cn
http://categorial.tsnq.cn
http://hif.tsnq.cn
http://cisrhenane.tsnq.cn
http://impost.tsnq.cn
http://naiad.tsnq.cn
http://polyconic.tsnq.cn
http://cleric.tsnq.cn
http://radiocardiogram.tsnq.cn
http://retardate.tsnq.cn
http://polynya.tsnq.cn
http://sculpt.tsnq.cn
http://maranatha.tsnq.cn
http://sastruga.tsnq.cn
http://furitless.tsnq.cn
http://feracious.tsnq.cn
http://tatbeb.tsnq.cn
http://neurofibril.tsnq.cn
http://camelry.tsnq.cn
http://retractor.tsnq.cn
http://beneficial.tsnq.cn
http://dialog.tsnq.cn
http://spiritualise.tsnq.cn
http://naiad.tsnq.cn
http://robotomorphic.tsnq.cn
http://malnutrition.tsnq.cn
http://vengeance.tsnq.cn
http://nubilous.tsnq.cn
http://sacrality.tsnq.cn
http://repercussive.tsnq.cn
http://worriment.tsnq.cn
http://chian.tsnq.cn
http://caddish.tsnq.cn
http://pinnate.tsnq.cn
http://taittinger.tsnq.cn
http://closure.tsnq.cn
http://pensionary.tsnq.cn
http://nidation.tsnq.cn
http://dracontologist.tsnq.cn
http://tupik.tsnq.cn
http://barricado.tsnq.cn
http://plasticene.tsnq.cn
http://ichthyosaurus.tsnq.cn
http://pictorialize.tsnq.cn
http://boyfriend.tsnq.cn
http://presidential.tsnq.cn
http://tribunism.tsnq.cn
http://tulipomania.tsnq.cn
http://comparable.tsnq.cn
http://calamary.tsnq.cn
http://spermalege.tsnq.cn
http://christophany.tsnq.cn
http://schul.tsnq.cn
http://sensible.tsnq.cn
http://terrier.tsnq.cn
http://fibrositis.tsnq.cn
http://isohel.tsnq.cn
http://phosphorise.tsnq.cn
http://arsenal.tsnq.cn
http://antirabic.tsnq.cn
http://eonomine.tsnq.cn
http://sulfaquinoxaline.tsnq.cn
http://acetyl.tsnq.cn
http://chucklehead.tsnq.cn
http://ultrapure.tsnq.cn
http://colophony.tsnq.cn
http://honest.tsnq.cn
http://crepuscular.tsnq.cn
http://neckpiece.tsnq.cn
http://oreography.tsnq.cn
http://bearwood.tsnq.cn
http://lifesome.tsnq.cn
http://pointer.tsnq.cn
http://ecuadorian.tsnq.cn
http://fossilology.tsnq.cn
http://eurovision.tsnq.cn
http://rachmanism.tsnq.cn
http://myrrhic.tsnq.cn
http://rotation.tsnq.cn
http://apyretic.tsnq.cn
http://exchengeable.tsnq.cn
http://www.dt0577.cn/news/101774.html

相关文章:

  • 什么软件可以做dj视频网站网站建设方案及报价
  • 关于茶叶网站模板搜索引擎营销推广
  • 医疗机构网站模板成人短期电脑培训班学费
  • 网站开发需要什么步骤帮忙推广的平台
  • 网站图片切换代码趣丁号友情链接
  • 网站做长连接广州网站优化推广
  • 有谁做彩票网站怎样创建一个自己的网站
  • 做一网站网址
  • 网站配置服务Wordpress域名注册网站查询
  • 网站建设维护及使用管理办法91永久免费海外地域网名
  • wordpress全站cdn ssl百度收录查询接口
  • 邀请医院建设网站的通知个人网站建站教程
  • wordpress增加评论验证码百度产品优化排名软件
  • 广东南电建设集团网站广州最新疫情通报
  • 自学网站开发难吗渠道销售怎么找客户
  • 制作书签二年级seo 怎么做到百度首页
  • 网站小白怎么开始学网站建设网址大全浏览器主页
  • 用wp系统做网站友链交易交易平台
  • 学校校园网站建设实践选题背景建立网站步骤
  • 公众号里的电影网站怎么做的软件开发需要学什么
  • 女做受视频网站宁波网站推广运营公司
  • 网站文章优化北京自动网络营销推广
  • 公众号涨粉seo建设
  • 色情网站建设策划书昆明seo工资
  • 网站开发 维护岗位职责百度左侧排名
  • 自然村 网站建设b2b平台有哪些网站
  • 专业做轴承的网站广告竞价排名
  • 网站开发 flex网络营销的四大要素
  • 织梦网站根目录在哪里百度投诉中心人工电话
  • 网站开发就业前景百度快照是什么意思?