LeetCode 15: 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.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie,
    abc)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

与Two Sum和 4 Sum类似,算法如下:

1、先对数组nums进行排序

2、对数组中的每个元素下表i,begin=i+1, end=length-1,sum = nums[i] + nums[begin] + nums[end]

sum == 0, 将三个元素组成vector,插入到结果vector中, begin++

sum < 0. begin++

sum >0  end--

要注意对重复元素的处理,避免产生重复元素。

nums[i] == nums[i-1],

nums[begin] == nums[begin-1]

nums[end] == nums[end+1]

代码如下:

	vector<vector<int>> threeSum(vector<int>& nums) {
		vector<vector<int>> result;
		int length = nums.size();
		if (length < 3 )
			return result;
		//先排序
		sort(nums.begin(), nums.end());
		for (int i=0; i<length-2; i++)
		{
			int begin = i+1;
			int end = length -1;
			//nums[i]和nums[i-1]相等,避免重复
			if (i>0 && nums[i]==nums[i-1])
				continue;

			while(begin <end)
			{
				//nums[begin]和nums[begin-1]相等,避免重复
				if (begin>i+1 && nums[begin]== nums[begin-1])
				{
					begin++;
					continue;
				}
				//nums[end]和nums[end+1]相等,避免重复
				if (end<length-1 && nums[end] == nums[end+1])
				{
					end--;
					continue;
				}
				int sum = nums[i] + nums[begin] + nums[end];
				if (sum < 0)
				{
					begin ++;
				}else if (sum == 0)
				{
					vector<int> tmp;
					tmp.push_back(nums[i]);
					tmp.push_back(nums[begin]);
					tmp.push_back(nums[end]);
					result.push_back(tmp);
					begin++;
				}else{
					end--;
				}

			}
		}
		return result;
	}
时间: 2024-08-02 07:38:27

LeetCode 15: 3 Sum的相关文章

LeetCode OJ:Range Sum Query 2D - Immutable(区域和2D版本)

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Example: Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7

leetcode笔记:Combination Sum II

一. 题目描述 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 once number of times. Note: ? All numbers (including target)

leetcode笔记:Range Sum Query 2D - Immutable

一. 题目描述 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (

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] ] 二.题目分析 这道题与Path Su

leetcode笔记: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 tr

LeetCode OJ:Two Sum(两数之和)

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

leetcode算法:Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

第一个leetcode题目: two sum

第一个leetcode,大概意思:在一个数组里,任意找两个数字,让它们的和等于一个给定目标值.要求:1.输出这两个数在数组的下标 2.保证第一个数的下标要小于第二个数的下标 做这个题,还是有一点点欣慰的地方就是:读完题,就有思路,而且能顺利编出来.(虽然因为那个小小的 return b 修改了好几次) public class Solution { public int[] twoSum(int[] nums, int target) { int[] b = new int[2]; for(in

LeetCode OJ:Combination Sum II (组合之和 II)

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 posi