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

三数之和与四数之和 | LeetCode-15 | LeetCode-18 | 双指针 | 降维 | 哈希集合 | Java | 详细注释

🙋大家好!我是毛毛张!
🌈个人首页: 神马都会亿点点的毛毛张
🍏去重和🍓降维是这两道题的关键!

📌LeetCode链接:15. 三数之和
📌LeetCode链接:18. 四数之和

文章目录

  • 题目1:三数之和🍇
    • 1.题目描述🍉
    • 2.题解🍋
      • 2.1 双指针🍑
  • 题目2:四数之和🍎
    • 1.题目描述🍍
    • 2.题解🥝
      • 2.1 双指针🥥

题目1:三数之和🍇

1.题目描述🍉

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。

注意: 答案中不可以包含重复的三元组。

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • − 1 0 5 < = n u m s [ i ] < = 1 0 5 -10^5 <= nums[i] <= 10^5 105<=nums[i]<=105

2.题解🍋

2.1 双指针🍑

class Solution {public List<List<Integer>> threeSum(int[] nums) {// 对数组进行排序,方便后续使用双指针方法寻找满足条件的三元组Arrays.sort(nums);// 用于存放所有满足条件的三元组结果List<List<Integer>> resultList = new ArrayList<>();int n = nums.length;// 第一层循环,选择第一个数for (int first = 0; first < n - 2; first++) {// 如果当前数字大于0,则不可能有三元组和为0,因为数组已排序if (nums[first] > 0) break;// 跳过重复的数字以避免添加重复的三元组if (first > 0 && nums[first] == nums[first - 1]) continue;// 定义双指针,second 从 first+1 开始,third 从数组末尾开始int second = first + 1, third = n - 1;// 双指针查找当前first对应的所有满足条件的三元组while (second < third) {// 计算三数之和int sum = nums[first] + nums[second] + nums[third];// 如果和为0,找到一个满足条件的三元组if (sum == 0) {List<Integer> list = new ArrayList<>();list.add(nums[first]);list.add(nums[second]);list.add(nums[third]);resultList.add(list);//resultList.add(Arrays.asList(nums[first], nums[second], nums[third]));// 去重:移动second和third指针以跳过重复的数字while (second < third && nums[second] == nums[second + 1]) second++;while (second < third && nums[third] == nums[third - 1]) third--;// 移动指针,继续寻找其他可能的三元组second++;third--;} else if (sum > 0) {// 如果和大于0,说明third指向的值太大,移动third指针减小总和third--;} else {// 如果和小于0,说明second指向的值太小,移动second指针增加总和second++;}}}// 返回所有找到的三元组列表return resultList;}
}

题目2:四数之和🍎

1.题目描述🍍

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abcd 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]

提示:

  • 1 <= nums.length <= 200
  • − 1 0 9 < = n u m s [ i ] < = 1 0 9 -10^9 <= nums[i] <= 10^9 109<=nums[i]<=109
  • − 1 0 9 < = t a r g e t < = 1 0 9 -10^9 <= target <= 10^9 109<=target<=109

2.题解🥝

2.1 双指针🥥

class Solution {public List<List<Integer>> fourSum(int[] nums, int target) {// 对数组进行排序,方便后续使用双指针方法寻找满足条件的四元组Arrays.sort(nums);// 用于存放所有满足条件的四元组结果List<List<Integer>> resultList = new ArrayList<>();int n = nums.length;// 第一层循环,选择第一个数for (int first = 0; first < n - 3; first++) {// 跳过重复的数字以避免添加重复的四元组if (first > 0 && nums[first] == nums[first - 1]) continue;// 第二层循环,选择第二个数for (int second = first + 1; second < n - 2; second++) {// 跳过重复的数字以避免添加重复的四元组if (second > first + 1 && nums[second] == nums[second - 1]) continue;// 定义双指针,third 从 second+1 开始,fourth 从数组末尾开始int third = second + 1;int fourth = n - 1;// 双指针查找当前first和second对应的所有满足条件的四元组while (third < fourth) {// 计算四数之和,使用long以防止整数溢出long sum = (long) nums[first] + nums[second] + nums[third] + nums[fourth];// 如果和等于目标值,找到一个满足条件的四元组if (sum == target) {List<Integer> list = new ArrayList<>();list.add(nums[first]);list.add(nums[second]);list.add(nums[third]);list.add(nums[fourth]);resultList.add(list);// 去重:移动third和fourth指针以跳过重复的数字while (third < fourth && nums[third] == nums[third + 1]) third++;while (third < fourth && nums[fourth] == nums[fourth - 1]) fourth--;// 移动指针,继续寻找其他可能的四元组third++;fourth--;} else if (sum > target) {// 如果和大于目标值,说明fourth指向的值太大,移动fourth指针减小总和fourth--;} else {// 如果和小于目标值,说明third指向的值太小,移动third指针增加总和third++;}}}}// 返回所有找到的四元组列表return resultList;}
}
  • 优化:
class Solution {public List<List<Integer>> fourSum(int[] nums, int target) {// 对数组进行排序,方便后续使用双指针方法寻找满足条件的四元组Arrays.sort(nums);// 用于存放所有满足条件的四元组结果List<List<Integer>> resultList = new ArrayList<>();int n = nums.length;// 第一层循环,选择第一个数for (int first = 0; first < n - 3; first++) {// 1级剪枝处理if(nums[first] > target && nums[first] >0) break;// 跳过重复的数字以避免添加重复的四元组if (first > 0 && nums[first] == nums[first - 1]) continue;// 第二层循环,选择第二个数for (int second = first + 1; second < n - 2; second++) {// 2级剪枝处理if (nums[first] + nums[second] > target && nums[first] + nums[second] >= 0) break;// 跳过重复的数字以避免添加重复的四元组if (second > first + 1 && nums[second] == nums[second - 1]) continue;// 定义双指针,third 从 second+1 开始,fourth 从数组末尾开始int third = second + 1;int fourth = n - 1;// 双指针查找当前first和second对应的所有满足条件的四元组while (third < fourth) {// 计算四数之和,使用long以防止整数溢出long sum = (long) nums[first] + nums[second] + nums[third] + nums[fourth];// 如果和等于目标值,找到一个满足条件的四元组if (sum == target) {resultList.add(Arrays.asList(nums[first], nums[second], nums[third], nums[fourth]));// 去重:移动third和fourth指针以跳过重复的数字while (third < fourth && nums[third] == nums[third + 1]) third++;while (third < fourth && nums[fourth] == nums[fourth - 1]) fourth--;// 移动指针,继续寻找其他可能的四元组third++;fourth--;} else if (sum > target) {// 如果和大于目标值,说明fourth指向的值太大,移动fourth指针减小总和fourth--;} else {// 如果和小于目标值,说明third指向的值太小,移动third指针增加总和third++;}}}}// 返回所有找到的四元组列表return resultList;}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Hive3:数据的加载与导出
  • 算法板子:欧拉函数——求一个数的欧拉函数、线性时间内求1~n所有数的欧拉函数
  • 宝兰德JVM参数查看及优化
  • 使用ubuntu串口数据收和发不一致问题
  • SpringAOP面向切面编程的概念和使用
  • 萱仔求职系列——1.1 机器学习基础知识复习
  • Redisson 实现分布式锁
  • vue2项目微信小程序的tabs切换效果
  • 学单片机怎么在3-5个月内找到工作?
  • 2024杭电多校(7) 1007. 创作乐曲【线段树预处理、dp、思维】
  • STM32的SDIO接口详解
  • 梅特勒金属探测器检测仪维修SAFELINE V3-QF1
  • 2024年电脑录屏软件推荐:捕捉屏幕,记录生活,分享精彩
  • Flink 实时数仓(八)【DWS 层搭建(二)流量域、用户域、交易域搭建】
  • 【JavaEE初阶】CAS(比较和交换)
  • angular2 简述
  • CAP理论的例子讲解
  • Create React App 使用
  • Cumulo 的 ClojureScript 模块已经成型
  • emacs初体验
  • ES学习笔记(12)--Symbol
  • Java精华积累:初学者都应该搞懂的问题
  • k8s 面向应用开发者的基础命令
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • Spring声明式事务管理之一:五大属性分析
  • vue数据传递--我有特殊的实现技巧
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 简单易用的leetcode开发测试工具(npm)
  • 利用DataURL技术在网页上显示图片
  • 前端学习笔记之观察者模式
  • 微信小程序实战练习(仿五洲到家微信版)
  • 我与Jetbrains的这些年
  • 赢得Docker挑战最佳实践
  • 用简单代码看卷积组块发展
  • raise 与 raise ... from 的区别
  • ​探讨元宇宙和VR虚拟现实之间的区别​
  • # linux 中使用 visudo 命令,怎么保存退出?
  • # 数据结构
  • #NOIP 2014# day.2 T2 寻找道路
  • #QT(智能家居界面-界面切换)
  • (31)对象的克隆
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (独孤九剑)--文件系统
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转) RFS+AutoItLibrary测试web对话框
  • ..回顾17,展望18
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .Net Core与存储过程(一)
  • .NET Core中的去虚