【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;
}

 

基于线性光耦HCNR200的高压直流采样电路设计与精度优化 本文详细介绍了基于线性光耦HCNR200的高压直流采样电路设计与精度优化方法。针对直流高电压测量中的安全与干扰问题,提出采用HCNR200光耦隔离方案,实现高精度线性传输。文章深入解析电路设计关键细节,包括电阻网络、运放选型、滤波电容配置等,并提供精度优化实战技巧与常见问题排查指南,助力新能源电动汽车和高压储能系统的高压直流采样应用。 阅读详情

相关推荐

Exchange 2013/2016/2019修改附件大小限制

Exchange 2013/2016/2019邮件大小限制 可以在三个位置上配置Exchange上的默认邮件大小限制: 组织传输设置 发送/接收连接器设置 用户邮箱设置 1.打开Exchange Management Shell 使用命令查看默认限制 get-transportconfig | ft maxsendsize, maxreceivesize get-receiveconnector | ft name, maxmessagesize get-sendconnector | ft

高级基础架构专家-Hunter 6107

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

Colab虚拟环境配置全攻略:从安装到实战应用

本文详细介绍了在Google Colab中配置Python虚拟环境的完整流程与实战技巧。通过安装Anaconda、创建独立环境、解决常见错误及结合深度学习项目应用,帮助用户有效管理依赖、避免冲突,并利用环境持久化策略提升在Colab中的开发效率与项目可复现性。

weixin_29204121的博客 346

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

工业级越界行为识别数据集:YOLOv8/v9直接训练的围栏异常检测资源

越界行为识别是智能安防与工业安全中的基础视觉任务,其核心在于对人员、车辆等目标跨越物理边界(如围栏、警戒线、作业禁区)的精准检测与分割。该技术依赖于高质量实例分割标注、归一化坐标格式及场景鲁棒性设计,具备低延迟、高召回、强泛化等工程价值,广泛应用于智慧工地电子围栏、化工厂禁区告警、物流园区越界监控及变电站安全巡检等真实工业场景。本数据集以YOLOv8/v9为基准构建,提供万张实拍+合成图像、像素级Segmentation标注、分层划分与完备元数据,开箱即用,直击工业落地中数据清洗、格式转换与标注不一致等关键

weixin_30080745的博客 326

虎符+红明谷+ctfshow渔人杯赛后复现

目录标题虎符CTFWeb-签到MISC-你会日志分析吗红明谷CTFWeb-write_shellMISC-我的心是冰冰的ctfshow渔人杯签到抽奖感受下气氛神仙姐姐阿拉丁迷飘啊飘简单二维码我跟你拼了 虎符CTF Web-签到 参考详细分析PHP源代码后门事件及其供应链安全启示 根据文章提示,添加恶意请求头 User-Agentt: zerodiumphpinfo(); 可以看到phpinfo内容 继续构造,查看根目录下的文件 User-Agentt: zerodiumsystem('ls /');

Atkx's Blog 1989

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

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

WULH 1177

【黑马点评】项目知识点及面经整理

【黑马点评】项目知识点及面经整理

m0_52031708的博客 6613

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的博客 758

机器人控制基础:伺服驱动电机 PID 参数自适应控制的实现方法及研究

伺服电机PID参数自适应控制技术研究 摘要:针对伺服驱动电机在动态负载工况下传统PID控制性能下降的问题,本文研究了多种自适应控制方法。通过分析6种主流技术(包括模型参考自适应、模糊逻辑、神经网络等)的原理、实现步骤及适用场景,揭示了负载感知精度、参数调节实时性和系统稳定性三大技术难点,并提出了相应解决方案。以模糊自适应PID为例的实验验证表明,该技术能有效提升伺服系统在变负载条件下的控制性能。研究为工业应用中伺服电机的精确控制提供了重要参考。

start_up_go的博客 832

Python爬虫教程:抓取电商网站评论(以淘宝和京东为例)

通过本文的介绍,我们学会了如何使用Python编写爬虫,抓取淘宝和京东等电商平台的商品评论数据。我们介绍了如何使用电商平台提供的开放API以及如何通过模拟浏览器来抓取动态加载的数据。此外,我们还讨论了如何应对反爬虫技术,如何存储抓取的数据等。爬取电商平台的数据是一个强大的工具,能够为市场分析、消费者行为研究、产品优化等领域提供有力的支持。然而,在使用爬虫技术时,我们必须遵循法律法规,尊重平台的隐私政策,避免滥用数据。

2201_76125261的博客 1741

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

【查找单入口空闲区域】-python实现-附ChatGPT解析

时间限制:1s 空间限制: 256MB 限定语言:不限题目描述:给定一个 m x n 的矩阵,由若干字符X’和 O构成,"X表示该处已被占据,O表示该处空闲,请找到最大的单入口空闲区域。空闲区域是由连通的O组成的区域,位于边界的O可以构成入口,单入口空闲区域即有且只有一个位于边界的o作为入口的由连通的O组成的区域如果两个元素在水平或垂直方向相邻,则称它们是“连通”的。第一行输入为两个数字,第一个数字为行数m,第二个数字列数n,两个数字以空格分隔,1

sinat_29950703的博客 244

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的博客 551

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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...

我的博客 143

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

[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的博客 123

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的博客 364

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的博客 394

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、付费专栏及课程。

余额充值
⌂ 换网址