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

双指针题总结

双指针题总结

  • hot100
    • 移动零
    • 盛水最多的容器
    • 三数之和
    • 接雨水
    • 最小覆盖子串

hot100

移动零

题目链接:
283.移动零
代码:

class Solution {public void moveZeroes(int[] nums) {int slow = 0;for (int fast = 0; fast < nums.length; fast ++){if (nums[fast] != 0){nums[slow++] = nums[fast];}}for (int i = slow; i < nums.length; i ++){nums[i] = 0;}}
}

盛水最多的容器

题目链接:
11.盛水最多的容器
代码:

class Solution {public int maxArea(int[] height) {int res = 0;int left = 0, right = height.length - 1;while (left < right) {if (height[left] < height[right]) {res = Math.max(res, (right - left)*height[left]);left ++;}else {res = Math.max(res, (right - left)*height[right]);right --;}}return res;}
}

三数之和

题目链接:
15.三数之和
代码:

class Solution {public List<List<Integer>> threeSum(int[] nums) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(nums);for (int i = 0; i < nums.length; i ++){if (nums[i] > 0) {return res;}if (i > 0 && nums[i] == nums[i - 1]) {continue;}int left = i + 1;int right = nums.length - 1;while (left < right) {int sum = nums[i] + nums[left] + nums[right];if (sum > 0) {right --;} else if (sum < 0) {left ++;} else {res.add(Arrays.asList(nums[i], nums[left], nums[right]));while (left < right && nums[right] == nums[right - 1]) {right --;}while (left < right && nums[left] == nums[left + 1]) {left ++;}right --;left ++;}}}return res;}
}

接雨水

题目链接:
42.接雨水
代码:

class Solution {public int trap(int[] height) {int res = 0;int left = 0, right = height.length - 1;int maxLeft = 0, maxRight = 0;while (left < right) {maxLeft = Math.max(maxLeft, height[left]);maxRight = Math.max(maxRight, height[right]);if (height[left] < height[right]) {res += maxLeft - height[left];left ++;} else {res += maxRight - height[right];right --;}}return res;}
}

最小覆盖子串

题目链接:
76.最小覆盖子串
代码:

class Solution {public String minWindow(String s, String t) {Map<Character, Integer> need = new HashMap<>();Map<Character, Integer> has = new HashMap<>();int left = 0, right = 0;int valid = 0;int start = 0, minLen = Integer.MAX_VALUE;for (char c : t.toCharArray()) need.put(c,need.getOrDefault(c,0) + 1);while(right < s.length()){char r = s.charAt(right);right ++;if (need.containsKey(r)){has.put(r,has.getOrDefault(r,0) + 1);if (has.get(r).equals(need.get(r))) valid ++;}while (valid == need.size()){if (right - left < minLen){start = left;minLen = right - left;}char l = s.charAt(left);if (need.containsKey(l)){has.put(l,has.get(l) - 1);if (has.get(l) < need.get(l)) valid --;}left ++;}}return minLen == Integer.MAX_VALUE ? "" : s.substring(start,start + minLen);}
}
http://www.lryc.cn/news/433557.html

相关文章:

  • [数据集][目标检测]人脸口罩佩戴目标检测数据集VOC+YOLO格式8068张3类别
  • JVM3-双亲委派机制
  • 经典文献阅读之--DEviLOG(使用合成数据和真实世界数据的数据驱动占用网格映射基于Transformer的BEV方案量产方案)
  • ssh之登录服务器后,自动进入目录(四十七)
  • 如何看待IBM中国研发部裁员?
  • 计算机毕业设计选题推荐-土地承包管理系统-Java/Python项目实战(亮点:数据可视化分析、账号锁定、智能推荐)
  • 2024年高校辅导员考试题库及答案
  • 使用python对股票市场进行数据挖掘的书籍资料有哪些
  • Python 将字典转换为 JSON
  • 就服务器而言,ARM架构与X86架构有什么区别?各自的优势在哪里?
  • [论文笔记]Dimensionality Reduction by Learning an Invariant Mapping
  • 链表算法题(下)
  • UE4_后期处理_后期处理材质及后期处理体积二
  • Linux系统与高效进程控制的实战技巧
  • 陈文自媒体:抖音创作者伙伴计划,你不知道的几点!
  • 便携式气象仪器的主要特点
  • 【开源风云】从若依系列脚手架汲取编程之道(四)
  • 华为 HCIP-Datacom H12-821 题库 (15)
  • MT6895(天玑8100)处理器规格参数_MTK联发科平台方案
  • 从 0 开始搞定 RAG 应用系列(第一篇):构建简单 RAG
  • 接口(Interface)和端点(Endpoint)的区别
  • 小米汽车再陷“抄袭”争议,上汽高管直言“真不要脸”
  • VS C++ 加入dump实现崩溃日志 可以再崩溃的时候使用VS调试
  • Ubuntu22.04版本左右,开机自动启动脚本
  • 中秋之美——html5+css+js制作中秋网页
  • java设计模式day03--(结构型模式:代理模式、适配器模式、装饰者模式、桥接模式、外观模式、组合模式、享元模式)
  • Golang path/filepath包详解:高效路径操作与实战案例
  • 【Shiro】Shiro 的学习教程(四)之 SpringBoot 集成 Shiro 原理
  • 多线程篇(阻塞队列- PriorityBlockingQueue)(持续更新迭代)
  • strstr函数的使用和模拟实现