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  是反复的。

代码例如以下:


时间复杂度为: n^2(logn)

法三:因为2-sum在数组有序的情况下我们是能够O(n)的时间来解决的。于是直接使用已有的2-sum的代码,代码例如以下:

版权声明:本文博客原创文章,博客,未经同意,不得转载。

3 sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

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

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

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

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. hadoop-1.1.2 在Windows环境下的部署

    1:先安装Cygwin 参考http://blog.csdn.net/wind520/article/details/9223003 2:下载 3:解压在C:\cygwin\hadoop1 4:配置 ...

  2. TRIZ系列-创新原理-22-变害为利原理

     变害为利原理的详细表述例如以下:1)利用有害的因素(特别是环境中的)获得积极的效果:   有害无害不过相对的(时间,空间,人),将有害的因素通过一定的处理和转化,能够变有害为实用,比方废品回收, ...

  3. Unity 捕获IronPython脚本错误

    using System; using System.Collections.Generic; using System.IO; using System.Reflection; using Syst ...

  4. Swift语法学习之 类和结构体

    类和结构体 本页包括内容: 类和结构体对照 结构体和枚举是值类型 类是引用类型 类和结构体的选择 集合(collection)类型的赋值与复制行为 与其他编程语言所不同的是,Swift 并不要求你为自 ...

  5. Could Not Connect

    今天在写Quartz定时任务web应用时,访问项目页面出现Could Not Connect,后台也没报错. 然后我访问http://127.0.0.1:8080(Apache主页)的时候是正常的. ...

  6. LightOJ1010---Knights in Chessboard (规律题)

    Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...

  7. Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies

    B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...

  8. axWindowsMediaPlayer1获取音频长度

    OpenFileDialog openFileDialog1 = new OpenFileDialog { InitialDirectory = "c:\\", Filter = ...

  9. Oracle listener lsnrctl

    lsnrctl(Listener Control)是一家SQL*Net具,用于控制数据库listener,此工具提供了控制命令listener开端.停止,查看listener状态,更改listener ...

  10. Spark第一个研究笔记1一片 - Spark一个简短的引论

    该公司推出的在线项目Spark拥有近1随着时间的推移.有效,Spark事实上,优秀的分布式计算平台,以提高生产力. 开始本篇笔记.此前的研究会Spark研究报告共享出来(由于篇幅的限制,它将被划分成制 ...