LeetCode -- Three Sum

问题描述: 在一个序列中找到3个数和等于N的所有组合(3个数升序排列,去重)
思路:
1 内循环使用two pointer ,从两端往中间找,判断和分别等于,小于,大于0的情况
2 外循环逐个后移
3 使用字典存键值=三个数的升序排列,解决重复问题

最坏情况: O(N2)

实现代码:

public IList<IList<int>> ThreeSum(int[] nums)
{
    if(nums == null || nums.Length < 3){
		return new List<IList<int>>();
	}

	var dic = new Dictionary<string, List<int>>();
	var list = nums.OrderBy(x=>x).ToList();

	var len = list.Count;
	for (var i = 0 ;i <= len - 3 ;i++){
		var a = list[i];
		var start = i+1;
		var end = len-1;
		while (start < end) {
		var b = list[start];
		var c = list[end];
		if (a+b+c == 0) {
			var v = new List<int>(){a,b,c}.OrderBy(x=>x).ToList();
			var k = string.Join(",",v);
			if(!dic.ContainsKey(k)){
				dic.Add(k,v);
			}
			start ++;
			end --;
		}
		else if (a+b+c > 0){
			end --;
		}
		else{
			start ++;
		}
	}
}

	var ret = new List<IList<int>>();
	foreach(var kv in dic){
		ret.Add(kv.Value);
	}

	return ret;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 20:59:04

LeetCode -- Three 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 -- 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 ≤ 

[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 =