n数之和
n 数之和
本质上就是将双指针作为等效循环(n 层循环 + 双指针
= n + 1 层循环
),实现复杂度降维
两数之和(核心)
排序 + 首尾双指针
vector<vector<int>> twoSum(vector<int> &nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
int left = 0, right = nums.size() - 1;
//要求两数之和不能取同一个数,故终止条件为 left < right
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
res.push_back({nums[left], nums[right]});
// 去重
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if (sum > 0) {
//大了,right 左移
right--;
} else {
//小了,left 右移
left++;
}
}
return res;
}
三数之和
vector<vector<int>> threeSum(vector<int> &nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
//内部复用两数之和的逻辑,但是左指针为外层遍历元素的下一个元素
int left = i + 1, right = nums.size() - 1;
while (left < right) {
//符合要求,处理结果
if (nums[i] + nums[left] + nums[right] == target) {
//获取第一个三元组后再做剪枝,防止漏掉[0, 0, 0]的情况
res.push_back({nums[i], nums[left], nums[right]});
// 去重
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if (nums[i] + nums[left] + nums[right] > target) {
//大了,right 左移
right--;
} else {
//小了,left 右移
left++;
}
}
}
return res;
}
四数之和
注意防止求和溢出
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for (int k = 0; k < nums.size(); k++) {
// 对nums[k]去重
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
for (int i = k + 1; i < nums.size(); i++) {
// 对nums[i]去重
if (i > k + 1 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left) {
long sum = (long) nums[k] + nums[i] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]});
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
}
return result;
}
n 数之和(推广)
在外面再套一层 for 循环即可,注意控制非最外层循环起点为 上一层循环变量 + 1
//for 循环参考
for (int k = 0; k < nums.size(); k++) {
// 对nums[k]去重
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
}
代码汇总
#include <iostream>
using namespace std;
//region 规范化输出
void printVector(vector<int> &nums) {
cout << "[";
for (int i = 0; i < nums.size(); i++) {
cout << nums[i];
if (i != nums.size() - 1) {
cout << ", ";
}
}
cout << "]";
}
void printResult(vector<vector<int>> &res) {
cout << "[";
for (int i = 0; i < res.size(); i++) {
printVector(res[i]);
if (i != res.size() - 1) {
cout << ", ";
}
}
cout << "]";
}
//endregion
vector<vector<int>> twoSum(vector<int> &nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
int left = 0, right = nums.size() - 1;
//要求两数之和不能取同一个数,故终止条件为 left < right
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
res.push_back({nums[left], nums[right]});
// 去重
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if (sum > 0) {
//大了,right 左移
right--;
} else {
//小了,left 右移
left++;
}
}
return res;
}
vector<vector<int>> threeSum(vector<int> &nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
//内部复用两数之和的逻辑,但是左指针为外层遍历元素的下一个元素
int left = i + 1, right = nums.size() - 1;
while (left < right) {
//符合要求,处理结果
if (nums[i] + nums[left] + nums[right] == target) {
//获取第一个三元组后再做剪枝,防止漏掉[0, 0, 0]的情况
res.push_back({nums[i], nums[left], nums[right]});
// 去重
while (left < right && nums[left] == nums[left + 1]) left++;
while (left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if (nums[i] + nums[left] + nums[right] > target) {
//大了,right 左移
right--;
} else {
//小了,left 右移
left++;
}
}
}
return res;
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for (int k = 0; k < nums.size(); k++) {
// 对nums[k]去重
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
for (int i = k + 1; i < nums.size(); i++) {
// 对nums[i]去重
if (i > k + 1 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left) {
long sum = (long) nums[k] + nums[i] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]});
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
}
return result;
}
int main() {
vector<int> nums = {-1, 0, 1, 2, -1, -4};
printVector(nums);
cout << endl;
int target = 0;
vector<vector<int>> res = twoSum(nums, target);
printResult(res);
cout << endl;
res = threeSum(nums, target);
printResult(res);
cout << endl;
res = fourSum(nums, target);
printResult(res);
cout << endl;
return 0;
}