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. android-adb通用

  2. B. Dreamoon and WiFi(Codeforces Round 272)

    B. Dreamoon and WiFi time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Maximal Square 我们都在寻找最高1子矩阵(leeCode)

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  4. DHCP Option 60 认识

    原文地址: http://blog.163.com/chenqioulin_1983/blog/static/83216232010109104430251/   首先还是看看RFC咋说的吧.DHCP ...

  5. 【iOS】随机三角瓷砖布局算法

    你已经看够iOS鉴于这些默认的正方形块,整齐地显示? 本篇给出一个随机算法设计的三角布局的瓷砖和实施. 这样的规则,并提出妥协随机排列间.它看起来很凌乱,不会有一个新事物. 重点是设计和实施,以实现布 ...

  6. 原生javascript学习

    首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利 ...

  7. Highcharts中文教程

    http://www.hcharts.cn/docs/index.php?doc=start

  8. ReactJS学习 相关网站

    React 入门实例教程-阮一峰 http://www.ruanyifeng.com/blog/2015/03/react.html汇智网-React 互动学习http://hubwiz.com/co ...

  9. 在线API大全

    之前整理过几个经常使用api地址,在经常使用在线API集合博文中. 近期浏览网页的时候,又发现一个很完整的api的大全,分享出来,建议大家收藏起来,用的时候方便查询. 经常使用API文档索引http: ...

  10. 流量计算-Jstorm提交Topology过程(下一个)

    马上部分流量计算-Jstorm提交Topology过程(上), 5.上篇任务已经ServiceHandler.submitTopologyWithOpts()方法.在该方法中,会实例化一个Topolo ...