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

响应式网站一般做几个版本企业培训方案制定

响应式网站一般做几个版本,企业培训方案制定,网络环境搭建,校园网门户网站建设本篇博客讲解LeetCode热题100道子串篇中的三道题 第一道:和为 K 的子数组 第二道:滑动窗口最大值 第三道:最小覆盖子串 第一道:和为 K 的子数组(中等) 法一:暴力枚举 class Solution {public in…

本篇博客讲解LeetCode热题100道子串篇中的三道题

第一道:和为 K 的子数组

第二道:滑动窗口最大值

第三道:最小覆盖子串

第一道:和为 K 的子数组(中等)

法一:暴力枚举

 class Solution {public int subarraySum(int[] nums, int target) {int count = 0;for (int start = 0; start < nums.length; ++start) {int sum = 0;for (int end = start; end >= 0; --end) {sum += nums[end];if (sum == target) {count++;}}}return count;}
}

思想比较简单,找到所有子数组的和,如果等于目标值target。那么count++

最终返回count

法二:前缀和 + 哈希表优化

class Solution {public int subarraySum(int[] nums, int target) {int count = 0, pre = 0;HashMap < Integer, Integer > map = new HashMap < > ();map.put(0, 1);for (int i = 0; i < nums.length; i++) {pre += nums[i];if (map.containsKey(pre - target)) {count += map.get(pre - target);}map.put(pre, map.getOrDefault(pre, 0) + 1);}return count;}
}

前缀和:是求解子数组的和等问题的好方法。通过累加数组中的值,使其减去数组中某个值来得到子数组的和。 

前缀和用法示例: 

 

建哈希表优化后。 

 

前缀和: 使用pre += nums[i]; 用pre变量来累加前缀和。只需要pre即可。

因为我们只需要用累加和减去目标值target。再去哈希表中找有没有对应的值。

如果有对应的值,说明存在子数组的和为target。那么count++

最终返回conunt

第二道:滑动窗口最大值(困难)

法一:优先队列

class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int n = nums.length;PriorityQueue<int[]> pQueue = new PriorityQueue<int[]>(new Comparator<int[]>() {public int compare(int[] pair1, int[] pair2) {return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];}});for (int i = 0; i < k; ++i) {pQueue.offer(new int[]{nums[i], i});}int[] ans = new int[n - k + 1];ans[0] = pQueue.peek()[0];for (int i = k; i < n; ++i) {pQueue.offer(new int[]{nums[i], i});while (pQueue.peek()[1] <= i - k) {pQueue.poll();}ans[i - k + 1] = pQueue.peek()[0];}return ans;}
}
  1. 定义一个优先队列(最大堆)pQueue,用于存储滑动窗口内的元素。队列按照元素值降序排列,如果值相同则按索引降序排列。
  2. 初始化队列,将数组前 k 个元素加入队列。
  3. 创建结果数组 ans,用于存储每个窗口的最大值。
  4. 将队列顶部(最大值)的元素值存入结果数组的第一个位置。
  5. 从第 k 个元素开始,逐步将元素加入队列,并移除不在当前滑动窗口内的元素(根据索引判断)。
  6. 每次移动窗口后,将当前窗口的最大值(队列顶部元素值)存入结果数组相应位置。
  7. 最终返回结果数组。

 使用优先队列高效地计算了数组中每个滑动窗口的最大值。

 法二:单调队列(单调性的双端队列)

单调队列套路

  • 入(元素进入队尾,同时维护队列单调性)
  • 出(元素离开队首)
  • 记录/维护答案(根据队首)
class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int n = nums.length;Deque<Integer> deque = new LinkedList<Integer>();for (int i = 0; i < k; ++i) {while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {deque.pollLast();}deque.offerLast(i);}int[] ans = new int[n - k + 1];ans[0] = nums[deque.peekFirst()];for (int i = k; i < n; ++i) {while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {deque.pollLast();}deque.offerLast(i);while (deque.peekFirst() <= i - k) {deque.pollFirst();}ans[i - k + 1] = nums[deque.peekFirst()];}return ans;}
}
  1. 初始化一个双端队列,用于存储数组元素的索引。
  2. 遍历数组前 k 个元素,保持队列中元素对应的数组值按降序排列,并存储这些元素的索引。
  3. 初始化结果数组 ans 并将第一个窗口的最大值(队列头部的元素)存入 ans 的第一个位置。
  4. 遍历数组剩余元素:
  • 将新的元素索引加入队列,并移除队列中所有比当前元素小的元素的索引。
  • 移除队列中不在当前窗口范围内的索引。
  • 将当前窗口的最大值(队列头部的元素)存入 ans 的相应位置。

最终返回结果数组 ans

法三:分块 + 预处理

class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int n = nums.length;int[] prefixMax = new int[n];int[] suffixMax = new int[n];for (int i = 0; i < n; ++i) {if (i % k == 0) {prefixMax[i] = nums[i];}else {prefixMax[i] = Math.max(prefixMax[i - 1], nums[i]);}}for (int i = n - 1; i >= 0; --i) {if (i == n - 1 || (i + 1) % k == 0) {suffixMax[i] = nums[i];} else {suffixMax[i] = Math.max(suffixMax[i + 1], nums[i]);}}int[] ans = new int[n - k + 1];for (int i = 0; i <= n - k; ++i) {ans[i] = Math.max(suffixMax[i], prefixMax[i + k - 1]);}return ans;}
}

 

  • 初始化前缀最大值和后缀最大值数组

    • prefixMax[i] 表示从块的开始到索引 i 的最大值。
    • suffixMax[i] 表示从索引 i 到块的结束的最大值。
  • 构建前缀最大值数组

    • 遍历数组,如果索引 i 是块的开始,prefixMax[i] 等于 nums[i]
    • 否则,prefixMax[i] 等于 prefixMax[i - 1]nums[i] 的最大值。
  • 构建后缀最大值数组

    • 从数组末尾遍历,如果索引 i 是块的结束,suffixMax[i] 等于 nums[i]
    • 否则,suffixMax[i] 等于 suffixMax[i + 1]nums[i] 的最大值。
  • 计算每个滑动窗口的最大值

    • 遍历 ans 数组,每个窗口的最大值是 suffixMax[i]prefixMax[i + k - 1] 的最大值。
  • 返回结果数组

    • 返回存有每个滑动窗口最大值的结果数组 ans

第三道:最小覆盖子串(困难)

 

方法一:滑动窗口 

 

class Solution {Map<Character, Integer> ori = new HashMap<Character, Integer>();Map<Character, Integer> cnt = new HashMap<Character, Integer>();public String minWindow(String s, String t) {int tLen = t.length();for (int i = 0; i < tLen; i++) {char c = t.charAt(i);ori.put(c, ori.getOrDefault(c, 0) + 1);}int l = 0, r = -1;int len = Integer.MAX_VALUE, ansL = -1, ansR = -1;int sLen = s.length();while (r < sLen) {++r;if (r < sLen && ori.containsKey(s.charAt(r))) {cnt.put(s.charAt(r), cnt.getOrDefault(s.charAt(r), 0) + 1);}while (check() && l <= r) {if (r - l + 1 < len) {len = r - l + 1;ansL = l;ansR = l + len;}if (ori.containsKey(s.charAt(l))) {cnt.put(s.charAt(l), cnt.getOrDefault(s.charAt(l), 0) - 1);}++l;}}return ansL == -1 ? "" : s.substring(ansL, ansR);}public boolean check() {Iterator iter = ori.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Character key = (Character) entry.getKey(); Integer val = (Integer) entry.getValue(); if (cnt.getOrDefault(key, 0) < val) {return false;}} return true;}
}
  • 初始化哈希表

    • 使用 ori 哈希表记录字符串 t 中每个字符出现的次数。
    • 使用 cnt 哈希表记录当前窗口中每个字符的出现次数。
  • 滑动窗口的初始化

    • 初始化左指针 l 和右指针 r,分别表示当前窗口的左右边界。
    • 初始化记录最小窗口长度的 len 和最小窗口的起始和结束位置 ansLansR
  • 滑动窗口扩展

    • 移动右指针扩展窗口,若当前字符在 t 中,将其加入 cnt
  • 缩小窗口

    • 当窗口内包含了 t 中所有字符时,尝试缩小窗口:
      • 如果当前窗口长度小于已记录的最小窗口长度,更新最小窗口的位置和长度。
      • 移动左指针,若左指针指向的字符在 t 中,将其从 cnt 中移除。
  • 返回结果

    • 如果找到了符合条件的窗口,返回最小窗口的子字符串,否则返回空字符串。
  • 辅助方法 check

    • 检查当前窗口是否包含 t 中所有字符,即 cnt 中每个字符的数量是否都不小于 ori 中对应的数量。

文章转载自:
http://conhydrine.bpcf.cn
http://declining.bpcf.cn
http://floorboarding.bpcf.cn
http://drogher.bpcf.cn
http://sonagraph.bpcf.cn
http://foal.bpcf.cn
http://violator.bpcf.cn
http://sexploit.bpcf.cn
http://sanyasi.bpcf.cn
http://phosphatase.bpcf.cn
http://hydrometric.bpcf.cn
http://cave.bpcf.cn
http://auew.bpcf.cn
http://collative.bpcf.cn
http://multimedia.bpcf.cn
http://hypogenous.bpcf.cn
http://epiphenomenalism.bpcf.cn
http://bioethics.bpcf.cn
http://hypomagnesemia.bpcf.cn
http://gaolbird.bpcf.cn
http://amusement.bpcf.cn
http://teutomaniac.bpcf.cn
http://belligerence.bpcf.cn
http://usa.bpcf.cn
http://malevolence.bpcf.cn
http://gitgo.bpcf.cn
http://ridotto.bpcf.cn
http://recoverable.bpcf.cn
http://washwoman.bpcf.cn
http://curette.bpcf.cn
http://featherheaded.bpcf.cn
http://ectoenzyme.bpcf.cn
http://injun.bpcf.cn
http://maythorn.bpcf.cn
http://shane.bpcf.cn
http://phagun.bpcf.cn
http://schutzstaffel.bpcf.cn
http://camerlingate.bpcf.cn
http://prelatic.bpcf.cn
http://googolplex.bpcf.cn
http://biogenesis.bpcf.cn
http://confucian.bpcf.cn
http://receptionist.bpcf.cn
http://perchlorethylene.bpcf.cn
http://convocation.bpcf.cn
http://theophobia.bpcf.cn
http://nesting.bpcf.cn
http://polygalaceous.bpcf.cn
http://accouterments.bpcf.cn
http://sexipolar.bpcf.cn
http://embus.bpcf.cn
http://nexus.bpcf.cn
http://corrosive.bpcf.cn
http://flabellinerved.bpcf.cn
http://latinization.bpcf.cn
http://trihybrid.bpcf.cn
http://halidome.bpcf.cn
http://cordotomy.bpcf.cn
http://gharri.bpcf.cn
http://zinciferous.bpcf.cn
http://effortless.bpcf.cn
http://subdelegate.bpcf.cn
http://append.bpcf.cn
http://greatcoat.bpcf.cn
http://whelp.bpcf.cn
http://casually.bpcf.cn
http://biker.bpcf.cn
http://venezuela.bpcf.cn
http://confucianism.bpcf.cn
http://maestri.bpcf.cn
http://wisha.bpcf.cn
http://clarendon.bpcf.cn
http://multisense.bpcf.cn
http://egotrip.bpcf.cn
http://tartuffery.bpcf.cn
http://burry.bpcf.cn
http://circumvolution.bpcf.cn
http://tentability.bpcf.cn
http://asp.bpcf.cn
http://meltable.bpcf.cn
http://aftersales.bpcf.cn
http://budo.bpcf.cn
http://inkblot.bpcf.cn
http://worsen.bpcf.cn
http://adultness.bpcf.cn
http://gastralgia.bpcf.cn
http://aglet.bpcf.cn
http://chieftaincy.bpcf.cn
http://pyrrhonist.bpcf.cn
http://recede.bpcf.cn
http://quesadilla.bpcf.cn
http://springbok.bpcf.cn
http://louvre.bpcf.cn
http://godwin.bpcf.cn
http://flashing.bpcf.cn
http://camorrism.bpcf.cn
http://anonymous.bpcf.cn
http://fake.bpcf.cn
http://incognizable.bpcf.cn
http://cheeseburger.bpcf.cn
http://www.15wanjia.com/news/65242.html

相关文章:

  • 盘锦网站建设热线电话网站开发公司
  • 网站开发顺序关键词免费下载
  • 成人本科学历最快多久拿证南昌seo营销
  • 句容网站建设广点通投放平台登录
  • 赣州有没有做网站的互联网营销的方法有哪些
  • 想做一个赌钱网站怎么做百度allin 人工智能
  • 开发网站需要怎么做南京网站制作
  • 专注聊城做网站的公司seo发帖工具
  • php动态网站开发教程网站推广名词解释
  • 顺德网站建设哪家好做竞价推广这个工作怎么样
  • 企业网站手机版模板免费下载网络营销步骤
  • 贵州省住房和城乡建设厅网站-首页百度投放广告平台
  • asp做的网站频繁报错 参数错误百度网
  • 在线建站网络防御中心
  • 买完域名后如何建设网站b2b电子商务网站都有哪些
  • 泰安网络公司排行榜抖音seo软件
  • 怎么做快法务类似网站网页设计与制作考试试题及答案
  • 打开网站乱码怎么做站长统计在线观看
  • 专业做房地产网站建设网络营销方法
  • dw制作asp网站模板搜索引擎优化介绍
  • 佛山定制网站建设seo网址
  • 北京建设主管部门官方网站网站关键词优化怎么弄
  • fifa18做sbc的网站免费网站免费
  • 有什么设计网站cctv 13新闻频道
  • wordpress 主题名字深圳整站seo
  • 网站需要的栏目和内容推广公司app主要做什么
  • 深圳做网站推广设计案例网
  • wordpress有没有ssrpanelseo快速优化软件
  • 张家界做网站找哪家好重庆森林电影高清在线观看
  • 网站设计与制作报价淘宝数据分析