一、前言
分类:Hash Table。
问题来源LeetCode 18 难度:中等。
问题链接:https://leetcode-cn.com/problems/4sum/
二、题目
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
三、思路
- 对数组中数据进行升序排序
- 第一个数从数组中第一个数往后查找
- 第二个数从第一个数往后查找
- 第三个数从第二个数往后查找
- 第四个数从数组尾往第三中数方向查找
四、编码实现
//==========================================================================
/*
* @file : 018_FourSum.h
* @blogs : https://blog.csdn.net/nie2314550441/article/details/107279832
* @author : niebingyu
* @date : 2020/07/11
* @title : 18.四数之和
* @purpose : 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,
* 使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
*
* 注意:
* 答案中不可以包含重复的四元组。
*
* 示例:
* 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
* 满足要求的四元组集合为:
* [
* [-1, 0, 0, 1],
* [-2, -1, 1, 2],
* [-2, 0, 0, 2]
* ]
*
*
* 来源:力扣(LeetCode)
* 难度:简单
* 链接:https://leetcode-cn.com/problems/4sum
*/
//==========================================================================
#pragma once
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
#define NAMESPACE_FOURSUM namespace NAME_FOURSUM {
#define NAMESPACE_FOURSUMEND }
NAMESPACE_FOURSUM
class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
vector<vector<int>> ret;
int len = (int)nums.size();
if (len < 4) return ret;
sort(nums.begin(), nums.end());
for(int i = 0;i < len - 3; i++)
{
if (i > 0 && nums[i] == nums[i-1]) continue;
if (nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target) break;
if (nums[i] + nums[len-1] + nums[len-2] + nums[len-3] < target) continue;
for (int j = i + 1; j < len - 2; j++)
{
if (j-i > 1 && nums[j] == nums[j-1]) continue;
if (nums[i] + nums[j+1] + nums[j+2] + nums[j] > target) break;
if (nums[i] + nums[len-1] + nums[len-2] + nums[j] < target) continue;
int left = j + 1;
int right = len - 1;
int complementSum = target - nums[i] + nums[j];
while (left < right)
{
int tmp = nums[left] + nums[right];
if (tmp == complementSum)
{
vector<int> vec;
vec.push_back(nums[i]);
vec.push_back(nums[j]);
vec.push_back(nums[left]);
vec.push_back(nums[right]);
ret.push_back(vec);
while (left < right && nums[left] == nums[left+1]) left++;
while (left < right && nums[right] == nums[right-1]) right--;
left += 1;
right -= 1;
}
else if (tmp > complementSum) right--;
else left++;
}
}
}
return ret;
}
};
以下为测试代码//
// 测试 用例 START
void test(const char* testName, vector<int>& nums, int target, int expect)
{
Solution s;
vector<vector<int>> result = s.fourSum(nums, target);
if (expect == result.size()) //粗略判断一下
cout << testName << ", solution passed." << endl;
else
cout << testName << ", solution failed." << endl;
}
// 测试用例
void Test1()
{
vector<int> nums = { 1, 0, -1, 0, -2, 2 };
int target = 0;
int expect = 3;
test("Test1()", nums, target, expect);
}
NAMESPACE_FOURSUMEND
// 测试 用例 END
//
void FourSum_Test()
{
NAME_FOURSUM::Test1();
}
执行结果: