15. 3Sum

Chapter2、HDR流水线 HDR内容的获取(不同曝光时间的图片合成) 相机响应函数 HDR视频 阅读详情

一开始是用回溯做的,结果超时,代码如下:

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
       vector<vector<int>> ans;
       sort(nums.begin(),nums.end()); //排序,便于剔除重复答案
       vector<int> temp;
       find(nums,ans,temp,0,0);       
       return ans;
        
    }
    void find(vector<int>& nums,vector<vector<int>>& ans, vector<int>& temp,int begin,int sum)
    {
        if(temp.size()==3&&sum==0)
        {         
            ans.push_back(temp);
            return ;
        }
        
        for(int i = begin;i<nums.size();i++)
        {
            if(i>begin&&nums[i]==nums[i-1])
                continue;  //去重
            temp.push_back(nums[i]);
            find(nums,ans,temp,i+1,sum+nums[i]);
            temp.pop_back();
            
        }
    }
};

于是改为时间复杂度为o(n*n)的“双指针”推广法:

class Solution {
public:
   vector<vector<int>> threeSum(vector<int>& nums) {
	 vector<vector<int>> ans;
	 vector<int> temp;
	 int i=0;
       if(nums.size()<3)
           return ans;
	 sort(nums.begin(),nums.end());
	 while(i<nums.size()-2)
	 {
		 int begin=i+1,end=nums.size()-1;
		 while(begin<end)
		 {
			 int sum=nums[i]+nums[begin]+nums[end];
			 if(sum==0)
			 {
				 temp.push_back(nums[i]);
                 temp.push_back(nums[begin]);
                 temp.push_back(nums[end]);
				 ans.push_back(temp);
				 temp.clear();
				 begin++;
				 end--;
				 while(nums[begin]==nums[begin-1])
					 begin++;
				 while(nums[end]==nums[end+1])
					 end--;
			 }
			 else
			 {
				 if(sum<0)
					 begin++;
				 else
					 end--;
			 }				 
		 }		 
		 i++;
		 while(nums[i]==nums[i-1])
			 i++;		 
	 }	 
	 return ans;
 }
};

STM32F407驱动OV2640摄像头:从SCCB协议到I2C模拟的保姆级避坑指南 本文详细介绍了STM32F407驱动OV2640摄像头的完整流程,重点解析SCCB协议与I2C的关键差异,并提供GPIO模拟实现方案。通过实战案例和调试技巧,帮助开发者避开常见陷阱,实现稳定的图像采集功能。 阅读详情

相关推荐

视频帧插学习(三):ema-vfi代码拆解和分析

video interp EMA-VFI

mikhailbran的博客 712

C++ sort函数用法

个人简单记录一下sort函数的用法,方便下次直接使用 sort包含在头文件<algorithm>中,用于对数组进行排序 sort可以有三个参数,sort(begin,end,cmp),第三个参数可以自己定义排序的规则,从而可以对二维数组进行排序等。 基本用法 数组定义: vector<int>nums; 默认:sort(nums.begin(),nums.end()); 对nums数组进行升序排序 自定义以进行降序排序 bool cmp(int x, int y) {

wikl的博客 6726

ADI DSP开发环境(CCES)下的程序烧录问题(以ADSP-SC589为例)

在CCES环境下将程序烧录到flash主要有两种方式。首先在对项目编译产生文件的属性设置为Release。 在编译完成后,找到每个CORE单独生成的DXE文件: 文件通常位于项目目录下每个core的Release文件中。安装SC589评估板的驱动(ADSP-SC5xx_EZ-KIT_Lite-Rel2.0.0.exe)后,在安装目录下可以找到SPI FLASH芯片的驱动程序(sc589_w25q128fv_dpia_Core1.dxe): 把文件copy到CCES 的目录下由于SC58x处理器的特殊性

daijingxin的博客 3731

C++中的自定义排序

sort用于C++中,对给定区间所有元素排序,在算法题时经常会用到sort排序。本文将会介绍几个常用的sort自定义排序。sort函数有三个参数sort(start,end,cmp),一般直接使用从小到大排序sort(nums.begin(), nums.end() [, less< data-type >]);或者从大到小排序sort(nums.begin(), nums.end(), grater< data-type >());

deletions的博客 4241

知识图谱的数据挖掘与分析

1. 背景介绍 1.1 数据挖掘的崛起 随着互联网的普及和大数据技术的发展,数据挖掘已经成为了计算机科学领域的热门研究方向。数据挖掘是从大量的、不完全的、有噪声的、模糊的、随机的实际应用数据中,通过计算机科学、统计学、信息论等多学科交叉的方法,挖掘出有用信息和知识的过程。数据挖掘的目标是从数据中发现有

AI天才研究院 1601

Zemax操作41--公差分析(一)

zemax公差

lzy750750的博客 5974

一文读懂快速排序

今天主要介绍下快速排序算法是如何实现的,主要的目标是:等很久之后忘了快速排序的思想时,找到这篇文章就可以帮助你很快的理解和回忆起来。 1.执行流程 1.从序列中选择一个轴点元素pivot从最后一个元素向前遍历 我们的策略是:每次选择第0位置的元素为轴点元素 2.利用pivot将数组分割成2个子数组 将小于pivot的元素放在pivot的左侧 将大于pivot的元素放在pivot的右侧 将等于pivot的元素放在pivot的哪侧.

Python+大数据+数据分析+自动化+Vue组件开发 299

银河麒麟硬盘健康检测:smartctl/badblocks实战与坏盘预判

xfs_repair检查。S.M.A.R.T检测。

信创运维工程师 · 银河麒麟 & 容器实战 | 金融生产环境迁移·等保加固 | 142篇原创·17万+阅读 860

sort()函数

使用该函数要加上一个头文件algorithm才能使用 该函数的功能:对给定范围内的序列进行升序排序(默认升序排列)。sort(begin,end,cmp),cmp参数默认升序。 vector容器函数进行排序时:sort(nums.begin(),nums.end()); 数组排序时一般为,示例: int a[7] = {1,11,9,6,8,3,2}; sort(a,a+7); ...

Bzdjsmz的博客 372

YOLO11-Seg优化:卷积变体系列篇 | 动态蛇形卷积(Dynamic Snake Convolution) | ICCV2023

动态蛇形卷积(Dynamic Snake Convolution),增强微小特征提取能力,引入到YOLO11-Seg,与C3k2结合实现二次创新

会AI的学姐 373

虚幻商城 Quixel 免费资产一键入库

在免费领取所有Megascans之后,我们只是有了获取 Quixel Megascans 资产的资格,但是没有入 Fab 库,本文将讲解通过脚本将 Quixel 所有资产入库 Fab。

一碗清深的博客 3359

C++vector容器使用sort排序(含升序以及降序)

当我们在使用C++STL容器时,难免会使用排序算法,我们不仅可以自己手写算法来实现,同时也可以使用

G_matinal的博客 7621

【图像去噪】基于加权核范数最小化算法实现图像去噪附matlab代码

1 简介 WNNM是一种约束矩阵奇异值稀疏性的低秩算法,在NNM理论基础上增加了核范数权重,有效利用了自然图像的先验信息并提高了核范数的灵活性. 2 部分代码 ​clcclear allclose allnSig = 100;O_Img = double(imread('Monarch.png'));​randn('seed', 0);N_Img = O_Img + nSig* randn(size(O_Img)); ..

qq_59747472的博客 3118

[leetcode]15. 3Sum

15. 3SumDescriptionAnalysisCode Description 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 ...

annazzz的博客 177

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[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

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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

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

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

leeetcode15. 3Sum

leetcode15. 3Sum 思路: 1.利用2sum的方法直接套上; 2.使用map,分情况讨论。

慕鹞 285

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

15. 3Sum 题解

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

yijingzhi的博客 303

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
上一篇: 【回溯】16. 3Sum Closest
下一篇: 18. 4Sum
云水谣CS
博客等级 码龄9年 8粉丝 268原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值