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. 采用SharePoint Designer将JavaScript而他们的网站页面集成的定义

    采用SharePoint Designer将JavaScript而他们的网站页面集成的定义 像JavaScript这种动态脚本语言可以给你的页面震撼效果.为了加盟JavaScript要定义自己的网站页 ...

  2. CSDN-markdown编者LaTex数学公式

    LaTex什么? LaTeX(LATEX.音译"拉太和")它是基于ΤΕΧ排版系统,莱斯利由美国计算机科学家·兰伯特(Leslie Lamport)于20纪80年代初期开发,利用这样 ...

  3. 系统负载测试工具-LoadRunner

    LoadRunner的主要作用是对系统压力测试进行分析 与之相类似的工具是:badboy:录制脚本工具+jmeter:分析结果工具

  4. Ubuntu14.04下安装ZendStudio10.6.1+SVN出现Failed to load JavaHL Library

    Subclipse不能正常工作,打开后报错: Failed to load JavaHL Library. These are the errors that were encountered: no ...

  5. 玩转Web之Json(二)----jquery easy ui + Ajax +Json+SQL实现前后台数据交互

    最近在学Json,在网上也找过一些资料,觉得有点乱,在这里,我以easy ui的登录界面为例来说一下怎样用Json实现前后台的数据交互 使用Json,首先需要导入一些jar包,这些资源可以在网上下载到 ...

  6. chrome使用技巧(转)good

    阅读目录 写在前面 快速切换文件 在源代码中搜索 在源代码中快速跳转到指定的行 使用多个插入符进行选择 设备模式 设备传感仿真 格式化凌乱的js源码 颜色选择器 改变颜色格式 强制改变元素状态(方便查 ...

  7. cocos2d-x v3.2 FlappyBird 各个类对象详细代码分析(6)

    今天我们要讲三个类,这三个类应该算比較简单的 HelpLayer类 NumberLayer类 GetLocalScore类 HelpLayer类,主要放了两个图形精灵上去,一个是游戏的名字,一个是提示 ...

  8. Memcached全面剖析–5. memcached的应用和兼容程序

    作者:长野雅广(Masahiro Nagano)  原文链接:http://gihyo.jp/dev/feature/01/memcached/0005 我是Mixi的长野.memcached的连载最 ...

  9. JSON连载java目的

    一. 前台(JS  面向对象) 1. 定义SearchView对象 function SearchView() { } SearchView.prototype.setViewName = funct ...

  10. SDUT 2498-AOE网上的关键路径(spfa+字典序路径)

    AOE网上的关键路径 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 一个无环的有向图称为无环图(Directed Acycl ...