3 sum
3-sum
标题叙述性说明:
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.
题目要求:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
每一个三元组内的元素是按非递减的顺序存放的,而且结果中要求不含有同样的集合。
解法:首先是排序。接着要求是不含有同样的集合,显然能够使用set,可是以下的代码所有都避免使用set。
以下一共使用了3种方法:
法一:DFS。复杂度高。而且在非常小的样例上都超时。
法二:枚举全部的2-sum和。
再在数组中查找是否存在另外一个数,使得该3个数的和为0.
此法不须要使用set,直接就能够得到结果,可是要注意避免反复计算,例如以下两点。
注1:上述的枚举2-sum时,对于剩下的那个数仅仅须要在 “下标都大于前两者时”进行。
例如以下例:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQzEyMzQ1U0RO/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="500" height="150" alt="">
在上图中,当枚举到i和j时,另外一个元素仅仅须要在 图示的 k 范围内枚举就可以。
注2:假设数组中有大量的反复元素,那么i和j(保持有A[i] == A[j])就仅仅须要考虑一次就可以。
例如以下例:
上图中。i 和 j仅仅须要考虑一次, 当 j 移动到 j‘ 的时候,是不须要考虑的,由于与前面的 i 和 j 是反复的。
代码例如以下:
版权声明:本文博客原创文章,博客,未经同意,不得转载。
3 sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 使用ZeroMQ(clrzmq)实现异步通信
ZeroMQ是对Socket的封装,通过组合多种类型的结点可以实现复杂的网络通信模式.而且ZeroMQ设计简单,可以有多种平台实现,对于跨平台项目是一个福音. clrzmq是ZeroMQ的C#语言的实 ...
- Lua相关的知识
http://stackoverflow.com/questions/5438751/how-to-debug-lua-remotely http://cn.bing.com/search?q=org ...
- 合作编辑室计费系统(一)-SVN常见错误
联合室已完成,在不到一个月的时间,我们的团队:嗤.陈琛.我.这段时间都挺辛苦的.从心里这次合作,真的让我们学习了非常多,学会了接纳和承担. 在我们開始合作机房的时候,社和师哥就给我们做了功课,说你们好 ...
- Cocos发展Visual Studio下一个libcurl图书馆开发环境的搭建
我们解释win32在Visual Studio下一个libcurl图书馆开发环境的搭建.Cocos2d-x发动机实际上与Win32在访问libcurl库.Cocos2d-x 3.x在libcurl库文 ...
- swift 笔记 (十二) —— 下标
下标 swift同意我们为 类.结构体,枚举 定义下标,以更便捷的方式訪问一大堆属性.比方Array和Dictionary都是结构体,swift的project师已经为这两个类型提供好了下标操作的代码 ...
- pdf转换为word小工具,挺好
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFwZW5nMDExMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Cocos2d-x 如何输出 Android用电话 腰带Tag的Log刊物
于Cocos2d-x根据代码 #if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #define LOGAnroid( ...) #else if (CC_TAR ...
- 【Android基础】短信的发送
//Button的点击事件 @Override public void onClick(View v) { // 接受者电话号码 Uri uri = Uri.parse("smsto:123 ...
- CMSIS标准
CMSIS 标准(Cortex Microcontroller Software Interface Standard) ,翻译过来是"ARM Cortex™ 微控制器软件接口标准" ...
- javascript倒置再次被否定作用
于javascript位反然后可以转换为浮点塑料,而不是更有效parseInt近两倍 var start = new Date().getTime(); for (var i = 0; i < ...