【LeetCode】15. 3Sum

Leetcode | C++ 15.3Sum 题意 中文描述 给定一个包含n个整数的数列S, 找出所有的独特的3个数列中的元素a,b,c使得a + b + c = 0。 注意: 结果集只包含不重复的三元组。 题解 算法及复杂度(382 ms) 本题与第1题(Two Sum)有类似之处,从题目上就可以看出来,第1题是两个数的和,而本题是求三个数的和。 由于要求三元组不能重复,所以可以想到进行排序,这样就可以很容易的发现重复的三元组。为什么通过排序容易发现重复结果?如果在无序的数列中进行操作,得到了满足a + b +c = 0之后,还需要对三元组的重复情况 阅读详情

参考TwoSum问题

15. 3Sum

Medium

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c= 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

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

1. 首先对vector排序。

2. 依次不重复取出每一个数nums[i](如果取出重复的数一定会导致重复的三元组)。

3. 对剩余的数,转换成TwoSum问题,target=-nums[i]。

4. 设置双指针left=i+1和right=nums.size()-1。

5. 如果nums[left]+nums[right]==target,则记录,并且跳过所有邻近的相同元素。

6. 如果大于,right--;如果小于,left++。

//https://leetcode.com/problems/3sum/discuss/7628/Share-my-C%2B%2B-solutionvery-easy-to-understand
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
vector<vector<int>> threeSum(vector<int>& nums) {
	vector<vector<int>> ret;
	int n = nums.size();
	if (n < 3)
		return ret;

	sort(nums.begin(), nums.end());

	int i = 0, left = 0, right = 0, target = 0;

	for (i = 0; i < n - 2; ++i)
	{
		//nums has been sorted in ascending order,if nums[i]>0, there are must not such triplets in the rear
		//thanks for lxyscls's advice
		if (nums[i] > 0)
			break;

		if (i != 0 && nums[i] == nums[i - 1])
			continue;

		target = 0 - nums[i];
		left = i + 1;
		right = n - 1;

		while (left < right)
		{
			if (nums[right] < 0)
				break;

			if ((nums[left] + nums[right]) == target)
			{
				vector<int> temp;
				temp.push_back(nums[i]);
				temp.push_back(nums[left]);
				temp.push_back(nums[right]);
				ret.push_back(temp);

				while (left < right && nums[left + 1] == nums[left])
					++left;
				while (left < right && nums[right - 1] == nums[right])
					--right;

				++left;
				--right;
			}
			else if ((nums[left] + nums[right]) < target)
				++left;
			else
				--right;
		}
	}

	return ret;
}
int main(void) {
	int n, elem; vector<int> input;
	while (cin >> n) {
		if (!input.empty()) input.pop_back();
		for (int i = 0; i<n; i++) {
			cin >> elem;
			input.push_back(elem);
		}
		vector<vector<int> > ans = threeSum(input);
		for (int i = 0; i<int(ans.size()); i++) {
			cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << endl;
		}
	}
	return 0;
}

 

微信群消息监控系统实战:从零搭建Python自动化监控平台 本文详细介绍了如何从零搭建Python微信群消息监控系统,实现自动化监控和记录群消息。通过wxauto库模拟人工操作微信,结合MySQL数据库存储和智能去重策略,系统能高效处理大量消息并支持关键词监控。文章还分享了性能优化、安全合规等实战经验,帮助开发者快速构建高效稳定的监控平台。 阅读详情

相关推荐

Mask2Former实战:用SwinTransformer+Deformable Attention搞定图像分割三大任务

本文实战解析了Mask2Former模型如何统一解决语义、实例和全景分割三大任务。通过集成Swin Transformer骨干网络与创新的Deformable Attention及Masked Attention机制,详细介绍了模型构建、训练策略及在COCO数据集上的性能表现,为开发者提供了高效的图像分割解决方案。

tech5的博客 246

LeetCode 15. 3Sum(三数之和)

原题网址:https://leetcode.com/problems/3sum/ Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of ze

James Pan 972

【CTFhub】——hate_php2021

【CTFhub】——hate_php2021

manerzi的博客 1605

15. 3Sum

class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<vector<int>> ret; int n=nums.size(); if(n<3)

专栏 351

遗传算法详解 附python代码实现

遗传算法 遗传算法是用于解决最优化问题的一种搜索算法。从名字来看,遗传算法借用了生物学里达尔文的进化理论:”适者生存,不适者淘汰“,将该理论以算法的形式表现出来就是遗传算法的过程。 问题引入 上面提到遗传算法是用来解决最优化问题的,下面我将以求二元函数: def F(x, y): return 3*(1-x)**2*np.exp(-(x**2)-(y+1)**2)- 10*(x/5 - x**3......

ha_ha_ha233的博客 22万+

HRNet 【demo 复现过程】

Deep High-Resolution Representation Learning for Human Pose Estimation

qq_45257495的博客 5556

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

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

WULH 1177

TDDFT计算软件Octopus学习笔记(二):基态计算(Si)

本文以硅为例,来进行基态计算。

陈啸的博客 7305

Leetcode15 3Sum(C++)

Leetcode15 3Sum(C++) 文章目录Leetcode15 3Sum(C++)题目论述思考分析code(c++) 题目论述 链接: Leetcode15 3Sum 概述: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all ...

cbb944131226的博客 756

PyTorch深度学习实战(29)——变分自编码器(Variational Autoencoder, VAE)

变分自编码器 (Variational Autoencoder, VAE) 是一种结合了自编码器和概率建模的生成模型,通过编码器将输入数据映射到潜在空间中的概率分布,并通过解码器将从潜在空间采样得到的潜在变量映射回原始数据空间,实现了数据的生成和特征学习。

盼小辉丶的博客 1万+

AI上推荐 之 逻辑回归模型与GBDT+LR(特征工程模型化的开端)

1. 前言 随着信息技术和互联网的发展, 我们已经步入了一个信息过载的时代,这个时代,无论是信息消费者还是信息生产者都遇到了很大的挑战: 信息消费者:如何从大量的信息中找到自己感兴趣的信息? 信息生产者:如何让自己生产的信息脱颖而出, 受到广大用户的关注? 为了解决这个矛盾, 推荐系统应时而生, 并飞速前进,在用户和信息之间架起了一道桥梁,一方面帮助用户发现对自己有价值的信息, 一方面让信息能够展现在对它感兴趣的用户前面。 推荐系统近几年有了深度学习的助推发展之势迅猛, 从前深度学习的传统推荐模型(协

Miracle8070 8251

LeetCode——15.3Sum

首次使用LeetCode学习算法设计,希望通过本次学习可以提高自己对各种算法的了解。 本次选择的题目是15.3Sum,属于medium难度。该题目的描述是"Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triple

parishong的博客 314

深入解析机器人软件开发中的核心:状态机管理(FSM)—— 理论、实践与优化

状态机(FSM)是机器人软件开发中管理复杂行为逻辑的核心工具,广泛应用于导航、任务执行、故障处理等场景。本文系统探讨状态机的基础理论(定义、要素)、主流设计模式(经典FSM、分层状态机HSM、行为树BT),以及工业级实现方法(代码架构、线程安全、调试技巧)。通过移动机器人导航和机械臂任务执行的案例,分析分层状态机的实际应用,并总结常见陷阱(如状态爆炸、阻塞操作)与优化策略(异步化、事件过滤)。

郑伟强dev的专栏 292

Leetcode 15. 3Sum (python)

题目 解法1:hash table + sort triplets 这解法T了,应该是T在遍历之前储存的pair那边。不过我个人感觉这种做法比较直观 class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: pairs = collections.defaultdict(list) n = len(nums) ans = set()

qq_37821701的博客 549

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Leetcode 15. 3Sum 三数之和

Leetcode 15. 3Sum 三数之和 标签(空格分隔): Leetcode 题目地址:https://leetcode-cn.com/problems/3sum/ 题目描述 给定一个包含 n 个整数的数组&nbsp;nums,判断&nbsp;nums&nbsp;中是否存在三个元素 a,b,c ,使得&nbsp;a + b + c = 0 ?找出所有满足条件且不重复的三元组。 注意:答案中...

CoderWangSon 196

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

leetcode记录15. 3Sum

leetcode记录15. 3Sum class Solution { public: vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) { sort(nums.begin(), nums.end()); vector&lt;vector&lt;int...

我的博客 141

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

[LeetCode] 15. 3Sum

原题链接: https://leetcode.com/problems/3sum/ 1. 题目描述 2. 解题思路 Java中List的用法 List是一个有序的集合,允许存储项的值为空,也允许相等值的存储项。 List&amp;amp;amp;amp;amp;lt;String&amp;amp;amp;amp;amp;gt; ex=new ArrayList&amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;gt;();

Ber03的博客 214

LeetCode 15. 3Sum

LeetCode 15. 3SumDescriptionExampleCodeConclusion Description Given an array numsof n integers, are there elements a, b, c in numssuch that a + b + c = 0? Find all unique triplets in the array which g...

gq__97的博客 205

LeetCode-15. 3Sum [C++]

Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.

贫道绝缘子的博客 443

【每日一题】Leetcode - 15. 3Sum

Leetcode - 15. 3Sum

qq_43209531的博客 321

[leetcode]15. 3Sum

[leetcode]15. 3Sum Analysis 中秋节了,中午要去吃海底捞—— [嘻嘻~] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives t...

weixin_32135877的博客 122

LeetCode 15: 3Sum题解(python)

Leetcode 42: Trapping Rain Water 分类:Two pointers 难度:M 描述:给了一个数组,问数组中存不存在a, b, c三个元素,使a + b + c = 0。把这些数组都找出来。 Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] 链接: 3Sum. 思路: 本题使用双指针法,遍历数组,然后每次遍历中使用前后指针搜索是否存在满足条件

Bourns_A的博客 362

Leetcode 15. 3Sum (Array)

Leetcode 15. 3Sum (Medium) (java) Tag: Array, Two Pointers Difficulty: Medium Discription:Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find a...

dengyao9100的博客 145

LeetCode15. 3Sum

LeetCode15. 3Sum 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The so

mohuak的博客 280

算法练习- LeetCode 15. 3Sum

算法练习- LeetCode 15. 3Sum

qq_41758969的博客 393

Leetcode 15. 3Sum

题目描述:找出任意三个数和为0的子集合,并列出出来,不可有重复。 题目链接:Leetcode 15. 3Sum 用递归的做法在31就超时了,一共313个case。 一般数组的题目要想加速 就要额外空间 hashmap做记忆或者两个指针的办法,一定要学会转化问题。 待定一个数 找这个数之后的数组所有不重复两个数相加和为此数的组合。 如果sum改成其他的数 只需要把求fix的步骤改一下就好了。 ...

Za七杂⑧ 196
上一篇: 【LeetCode】1. Two Sum
下一篇: 【LeetCode】2. Add Two Numbers
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值