leetcode -- 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时,对于剩下的那个数只需要在 “下标都大于前两者时”进行。如下例:

在上图中,当枚举到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的代码,代码如下:

leetcode -- 3 sum,布布扣,bubuko.com

时间: 2024-10-14 01:08:15

leetcode -- 3 sum的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

LeetCode: Path Sum II [113]

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [题意] 判断二叉树中是否存在一条从根到

[leetcode]Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum =