LeetCode | 18. 4Sum

Leetcode | C++ 18.4Sum 题意 中文描述 给定一个数列 S ,包含 n 个整数.在 S 中找到四个元素 a, b, c, d 使得 a + b + c + d = target . 找到所有的独特的满足上述条件的四元组. 注意: 结果集包含的四元组不重复. 题解 算法及复杂度(45 ms) 本题与第 15 题 (3Sum) 比较类似,第 15 题是求三个数的和,本题是求四个数的和.建议先去练习第 15 题. 首先,为了避免结果的重复,先对数列进行排序.依照在 15 题中提到的: 在一段数列上找到和为固定值的两个数的复杂度可以为 O( 阅读详情

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

跟2Sum 3Sum类似,用两个指针left , right 在后面遍历一遍,复杂度 O(n^3)
55ms Ac

class Solution {
public:
        vector<vector<int>> fourSum(vector<int>& nums, int target)
        {
            vector<vector<int> > res;
            vector<int> tmp;
            sort(nums.begin(),nums.end());
            int len = nums.size();
            for(int i=0;i<len;i++)
            {
                for(int j=i+1;j<len;j++)
                {
                    int left = j+1, right = len-1;
                    while(left < right)
                    {
                        int sum = nums[i]+nums[j]+nums[left]+nums[right];
                        if(sum == target)//找到目标
                        {
                            tmp.push_back(nums[i]); tmp.push_back(nums[j]);
                            tmp.push_back(nums[left]); tmp.push_back(nums[right]);
                            res.push_back(tmp);
                            while(left<len-1 && nums[left]==tmp[2])//不这样比较容易陷入死循环:因为left/right可能会没有移动
                            {
                                left++;
                            }
                            while(right>=1 && nums[right]==tmp[3])
                            {
                                right--;
                            }
                            tmp.clear();

                        }
                        else if(sum < target)
                        {
                            left++;
                        }
                        else
                        {
                            right--;
                        }
                        while(j+1<len && nums[j]==nums[j+1])
                        {
                            j++;
                        }
                    }
                }
                while(i+1<len && nums[i]==nums[i+1])
                {
                    i++;
                }
            }
            return res;
        }
};
从零开始写一个RTSP服务器(一)RTSP协议讲解 从零开始写一个RTSP服务器系列 从零开始写一个RTSP服务器(一)不一样的RTSP协议讲解 从零开始写一个RTSP服务器(二)RTP传输H.264(待写) 从零开始写一个RTSP服务器(三)一个传输H.264的RTSP服务器(待写) 从零开始写一个RTSP服务器(四)RTP传输AAC(待写) 从零开始写一个RTSP服务器(五)一个传输AAC的RTSP服务器(待写) 从零开始写一个RTSP服务器(... 阅读详情

相关推荐

企业和个人 AI 编程工具怎么选?2025 年 12 款主流工具(Claude/Cursor 等)核心能力 + 收费对比

2025 年最火的 12 款 AI 编程工具,像 Claude、Cursor 这些热门款,我把它们的核心能力、收费标准全扒得明明白白,看完这篇你不用再瞎琢磨,直接就能挑到对的!再说说 Cursor,美国 Cursor Labs 做的,还拿过红杉资本投资,2022 年推出后,2024 年收入直接涨了 6400%,势头很猛。总之,不管你是企业技术管理者、程序员,还是刚入门的 AI 小白,按自己的需求(语言、合规、预算)挑,保准能找到最适合的 AI 编程工具!最后给大家划重点,不同需求直接对号入座:?

hundred98的博客 2806

leetcode 18. 4Sum

''' 给定输入数组,输出4元对列表,列表中每个元素是一个列表,子列表数值之和为target ''' class Solution: def fourSum(self, nums, target: int): def select_sort(nums): # 对输入数据进行选择排序 # 选择排序算法是对冒泡排序算法的改进 ...

WYXHAHAHA123的博客 208

项目安全等保测评加固手册

安全等级测评的目的是通过对目标系统在安全技术及管理方面的测评,对目标系统的安全技术状态及安全管理状况做出初步判断,给出目标系统在安全技术及安全管理方面与其相应安全等级保护要求之间的差距。测评结论作为委托方进一步完善系统安全策略及安全技术防护措施依据!!

菜鸟运维升级之路 2419

LeetCode 力扣:15.3Sum + 16.3Sum Closest + 18.4Sum 排序 + 双指针遍历 题目+算法分析+Cpp解答

15.3Sum 排序 + 双指针遍历: ​ 首先,将给定的数组进行排序(从小到大的顺序)。 ​ 遍历排序后的数组中的每一个数,如果这个数之前已经讨论过了则可以跳过。 ​ 将取出的数作为“目标”,使用双指针在排序后的数组中找另外的两个数: ​ 一个左指针指向该取出的数的后一个数,即此时的最低位。 ​ 另一个右指针指向数组的最后一个数,即此时的最高位。 ​ 由于数组已经是从小到...

WULH 1177

Python表白代码:“ 星光月夜烟花皆归你,我也归你”(满天烟花盛开、附番外玫瑰)

导语 "慢品人间烟火色 闲观人间岁月长" ———致自己​​​ ​ ???? 遇见我以后,我们的故事就开始了,愿你历经山河,仍觉得人间值得????。 ​ 星光月夜烟花皆归你,我也归你。关于烟花????大家都​知道多少?有多少表白故事情节都发生在烟 花下,想必木木子????不用说大家也知道叭~​​ 今天这则小短文就是关于烟花的故事!你准备好跟我一起进入烟花的世界了嘛? ​正文 “每一句文案,都有一个故事,你仔细听” ​ 1)环境安装???? 准备好:.

顾木子吖的博客 5万+

【AMD显卡在WIndow10/11部署带GPU支持的深度学习环境(Pytorch-DirectML篇)】

这篇是之前Tensorflow-DirectML的姊妹篇,如果对TF感兴趣的可以搜索我的博文。 首先我们丢一个官网地址,但是官网那个会误导新人如果你按那个操作的话。 https://docs.microsoft.com/en-us/windows/ai/directml/gpu-pytorch-windows 这里用我的方法操作: 首先声明一下,因为我的显卡是AMD RX6600,不带XT(性能和RTX3060 12G版本半斤八两),所以目前没有最新的WSL2驱动,如果有了的话欢迎告知我,所以我这里就.

weixin_44029053的博客 2万+

leetcode 18:4Sum

class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ length = len(nums)...

dxcve的博客 177

电池保护板为什么采用NTC热敏电阻而不是PTC热敏电阻?

R25=10K,即环境温度25°时,阻值是10K),温度变化1°C,阻值相应地明显变化,温度响应很灵敏,也就能精准地反向换算出阻值对应的温度。另外,还有功率型NTC热敏电阻,其主要用在开关电源设计中,串联在市电输入上,抑制浪涌电流。电池保护板上的NTC热敏电阻(NTC一端接地,另一端接电池充电控制IC管脚),在电池温度上升时,NTC热敏电阻温度也会随之上升,从而NTC电阻值会下降,因此电池充电控制IC用ADC检测的电压值就降低,当检测到的电压值达到下限阈值时,电池充电控制IC就会停止对电池的充电或放电。

电子绿洲的博客 5632

leetcode 18 4Sum 详细解答

leetcode 18 4Sum 详细解答 代码如下:

qq_22210659的博客 205

【原理图PCB专题】原理图图纸锁定/解锁与PCB文件加密方式

在工作中我们会遇到需要冻结原理图进行评审和加密图纸防止被他人盗用的需求。那么在OrCAD Capture中如何对图纸进行锁定与解锁,如何在Allegro中对PCB工程进行加密呢?

一点硬件 3258

深度学习流体力学:基于PyTorch的物理信息神经网络(PINN)完整实现

本文介绍了基于物理信息神经网络(PINN)的深度学习流体力学求解器实现。首先阐述了Navier-Stokes方程的理论基础,包括连续性方程和动量守恒方程,并解释了PINN的核心原理:将物理方程作为约束条件嵌入神经网络损失函数。以经典的lid-driven cavity流动为例,详细展示了使用PyTorch实现的全流程,包括神经网络架构设计、自动微分求解Navier-Stokes方程、多约束条件优化训练等关键环节。该求解器通过将物理方程、边界条件和初始条件整合到损失函数中,实现了对流体力学问题的端到端求解,为

源代码杀手的博客 9846

leetcode18 4Sum

题目描述 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum o...

yaotai8135的博客 164

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode: 18. 4Sum

题目: LeetCode:18. 4Sum 描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of ...

dbb191379的博客 181

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【leetcode】18. 4Sum

题目: Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of...

zxc120389574的博客 93

LeetCode //C - 18. 4Sum

【代码】LeetCode //C - 18. 4Sum。

Made in Code 975

LeetCode 18. 4Sum

问题链接 LeetCode 18. 4Sum 题目解析 给定n元素的数组,求出满足 \(a+b+c+d = target\) 的所有元组。 解题思路 已经成为一个系列了,这道题是 LeetCode 15. 3Sum 的升级版,思路与其也是一模一样,外层再加一层循环。 先将数组排好序,便于检测重复。最外层循环固定最小数字 \(a\),第二层循环固定第二小数字 \(b\),左右指针分别代表 \(c\)...

weixin_30547797的博客 72

[leetcode]18. 4Sum

[leetcode]18. 4Sum Analysis 中秋节了,中午要去吃海底捞—— [嘻嘻~] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all uniq...

weixin_32135877的博客 104

【Leetcode- 18】4Sum

【Leetcode- 18】4Sum /*Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which...

zj20165149的博客 172

[ LeetCode ] #18. 4Sum(四数之和)

题目:18. 4Sum Difficulty: Medium Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the...

xn's blog 150

LeetCode: 18. 4Sum

LeetCode: 18. 4Sum 递归调用的例子,参考网上的实现https://discuss.leetcode.com/topic/46339/my-solution-generalized-for-ksums-in-java/5 class Solution { int len =0; public List&lt...

哎呀妈呀脑壳疼脑壳疼 117

Leetcode 18 4Sum

思路: two pointer。这题其实是3sum的变种。但是我们不可以他说要求4sum,我们就简单在3sum的代码外面再套一层循环。这不是长久之计,我们面试可能会被问到6sum,7sum。。。所以这题的主旨是要我们解决ksum的问题。这边其实思路还是two pointer,只不过我们要用到recursion。base case这边是2sum。其实就相当于,比方说要求5sum,那么就recursion,先4sum,再3sum,最后到2sum。这时候突然发现我们碰到base case了(我们可以直接解决的最.

GoodJobJasper的博客 183

【Leetcode】18. 4Sum

题目地址: https://leetcode.com/problems/4sum/ 给定一个数组AAA,再给定一个数ttt,

数学、算法爱好者的博客 218

[LeetCode 18] 4Sum

Given an arraynumsofnintegers and an integertarget, are there elementsa,b,c, anddinnumssuch thata+b+c+d=target? Find all unique quadruplets in the array which gives the sum oftar...

ExcitedZhang的博客 113

Leetcode题解-18. 4Sum

Leetcode题解-18. 4SumGiven an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:

hzw2945的博客 273
上一篇: LeetCode | 17. Letter Combinations of a Phone Number
下一篇: LeetCode | 19. Remove Nth Node From End of List
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值