[LeetCode]4 Sum

类似3Sum,先排序,这后两重for循环,然后对最后的一个数组设两个指针遍历。这里注意重复的问题,例如第一重如果和前面一个数相同则跳过,因为前面的查找肯定包含了本次的情况。

// 4Sum.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
class Solution {
public:
	vector<vector<int> > fourSum(vector<int> &num, int target) {
		sort(num.begin(),num.end());
		vector<vector<int>>res;
		if (num.size() < 4)
			return res;
		for (int i = 0; i < num.size()-3; i++)
		{
			if (i>0 && num[i] == num[i - 1])
				continue;
			for (int j = i + 1; j < num.size() - 2; j++)
			{
				if (j>i + 1 && num[j] == num[j - 1])
					continue;
				int p1 = j + 1,p2=num.size()-1;
				int sum = target - num[i] - num[j];
				while (p1 < p2)
				{
					if (p1>j + 1 && num[p1] == num[p1 - 1])
					{
						p1++;
						continue;
					}
					if (p2<num.size()-1&&num[p2] == num[p2 + 1])
					{
						p2--;
						continue;
					}
					if (p1 >= p2)
					{
						break;
					}
					if (num[p1] + num[p2] == sum)
					{

						vector<int>tempV;
						tempV.push_back(num[i]);
						tempV.push_back(num[j]);
						tempV.push_back(num[p1]);
						tempV.push_back(num[p2]);
						res.push_back(tempV);
						p2--;
					}
					else if (num[p1]+num[p2]>sum)
					{
						p2--;
					}
					else{
						p1++;
					}
				}
			}
		}
		return res;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	Solution ss;
	vector<int>test;
	test.push_back(1);
	test.push_back(0);
	test.push_back(-1);
	test.push_back(0);
	test.push_back(-2);
	test.push_back(2);
	vector<vector<int> > res = ss.fourSum(test,0);
	for (int i = 0; i < res.size(); i++)
	{
		for (int j = 0; j < res[i].size(); j++)
			cout << res[i][j] << ",";
		cout << endl;
	}
	system("pause");
	return 0;
}

  

时间: 2024-10-08 13:59:45

[LeetCode]4 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 =