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 ...
随机推荐
- Razor button
比起Web Form開發,在後端(.cs)寫法上大同小異,可選擇C#或VB.NET來撰寫:而在前端(.cshtml..vbhtml)則有比較大的差別,自 MVC3版本後,就以Razor為前端檢視引擎, ...
- 【iOS】随机三角瓷砖布局算法
你已经看够iOS鉴于这些默认的正方形块,整齐地显示? 本篇给出一个随机算法设计的三角布局的瓷砖和实施. 这样的规则,并提出妥协随机排列间.它看起来很凌乱,不会有一个新事物. 重点是设计和实施,以实现布 ...
- CSS设计指南之定位
原文:CSS设计指南之定位 CSS布局的核心是position属性,对元素盒子应用这个属性,可以相对于它在常规文档流中的位置重新定位.position属性有4个值:static.relative.ab ...
- Java AIO 入门实例(转)
Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...
- 房费制 之 登录BUG
声明:以下内容只有当你登录到一个username同时,学生不能申请多次登录. 说是BUG,事实上这也不是一个BUG,仅仅是想出一个办法,解决一个大家好多人都没有解决的问题.以下就给大家 ...
- Microsoft Toolkit 2.5下载 – 一键激活Windows 8.1/2012 R2/Office 2013
http://www.dayanzai.me/microsoft-toolkit-2-5.html
- groovy : poi 导出 Excel
參考 poi-3.10-FINAL/docs/spreadsheet/quick-guide.html write_xls.groovy 代码例如以下 package xls; import java ...
- Java的socket服务UDP协议
练习1 接收类 package com.socket.demo; import java.io.IOException; import java.net.DatagramPacket; import ...
- 关系数据库的基本概念和MySQL说明
关系数据库的基本概念 数据库: 大量的信息化解决方案的高效管理. 根据数据结构来组织.存储和管理数据的库. 数据库系统(DBS,DATABASE SYSTEM): 数据库(DB,DATABASE) + ...
- ASP.NET MVC 5– 采用Wijmo MVC 5模板1创建应用程序分钟
启用 采用ComponentOne Studio for ASP.NET Wijmo制作MVC5应用,首先要做的就是安装pid=4&from=MVC4DOC">Studio f ...