【LeetCode】10.4Sum

题目描述(Medium)

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:
The solution set must not contain duplicate quadruplets.

题目链接

https://leetcode.com/problems/4sum/description/

Example 1:

 Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

 A solution set is:
 [
     [-1,  0, 0, 1],
     [-2, -1, 1, 2],
     [-2,  0, 0, 2]
 ]

算法分析

法一:先排序,然后左右夹逼,复杂度O(n^3)

法二:用一个HashMap缓存两个数的和,复杂度O(n^2)

Tip:最终案例运行时间,法一快于法二,因为法二虽然时间复杂度较低,但是中间使用了较多的查找、排序的STL,因此总体运行时间反而较长。

提交代码(方法一):

class Solution {
public:
	vector<vector<int>> fourSum(vector<int>& nums, int target) {
		vector<vector<int>> result;
		if (nums.size() < 4) return result;
		sort(nums.begin(), nums.end());

		for (auto i = nums.cbegin(); i < nums.cend() - 3; ++i)
		{
			for (auto j = i + 1; j < nums.cend() - 2; ++j)
			{
				auto k = j + 1, l = nums.cend() - 1;
				while (k < l)
				{
					if ((*i + *j + *k + *l) < target)
					{
						++k;
					}
					else if ((*i + *j + *k + *l) > target)
					{
						--l;
					}
					else
					{
						result.push_back({*i, *j, *k, *l});
						++k, --l;
					}
				}
			}
		}
		sort(result.begin(), result.end());
		result.erase(unique(result.begin(), result.end()), result.end());
		return result;
	}
};

提交代码(方法二):

class Solution {
public:
	vector<vector<int>> fourSum(vector<int>& nums, int target) {
		vector<vector<int>> result;
		if (nums.size() < 4) return result;
		sort(nums.begin(), nums.end());

		unordered_multimap<int, pair<int, int> > cache;
		for (int i = 0; i < nums.size() - 1; ++i)
			for (int j = i + 1; j < nums.size(); ++j)
				cache.insert(make_pair(nums[i] + nums[j], make_pair(i, j)));

		for (auto i = cache.cbegin(); i != cache.cend(); ++i)
		{
			auto range = cache.equal_range(target - i->first);
			for (auto j = range.first; j != range.second; ++j)
			{
				int a = i->second.first;
				int b = i->second.second;
				int c = j->second.first;
				int d = j->second.second;

				if (a != c && a != d && b != c && b != d)
				{
					vector<int> temp = { nums[a], nums[b], nums[c], nums[d] };
					sort(temp.begin(), temp.end());
					result.push_back(temp);
				}
			}
		}

		sort(result.begin(), result.end());
		result.erase(unique(result.begin(), result.end()), result.end());
		return result;
	}
};

测试代码:

// ====================测试代码====================
void Test(const char* testName, vector<int>& nums, int target, vector<vector<int>>& expected)
{
	if (testName != nullptr)
		printf("%s begins: \n", testName);

	Solution s;
	vector<vector<int>> result = s.fourSum(nums, target);

	if (result == expected)
		printf("passed\n");
	else
		printf("failed\n");

}

int main(int argc, char* argv[])
{

	vector<int> array1 = { 1, 0, -1, 0, -2, 2 };
	vector<vector<int> > result = {
		{ -1,  0, 0, 1 },
		{ -2, -1, 1, 2 },
		{ -2,  0, 0, 2 }
	};
	Test("Test1", array1, 0, result);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ansizhong9191/article/details/81983520
今日推荐