2sum:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解答过程:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;//用来存储已经检测过的数字
vector<int> ret;
for (int i = 0; i < nums.size(); ++i) {
//判断numToFind是否在hash容器中
if (hash.find(target - nums[i]) != hash.end()) {
ret.push_back(hash[target - nums[i]]);
ret.push_back(i);
return ret;
}
hash[nums[i]] = i;
}
return ret;
}
3sum:
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
解答代码(此想法受启发于4sum的解答过程)
vector<vector<int>> threeSum(vector<int>& num) {
sort(num.begin(), num.end());//从小到大进行排序
vector<vector<int>> ret;
int target = INT_MIN;
int sec, thi;
for (int i = 0; i < num.size(); ++i) {
while (target == -num[i]) ++i;//避免重复值
target = -num[i];
sec = i + 1;
thi = num.size() - 1;
vector<int> tmp(1, num[i]);
while (sec < thi) {//从num[sec]和num[thi]往中间夹,观察和值与target比较,进行变换sec和thi的值
if (num[sec] + num[thi] < target) {//从前面递增
while (num[sec] == num[sec+1]) ++sec;//避免重复值
++sec;
}
else if (num[sec] + num[thi] > target) {//从后面递减
while (num[thi] == num[thi - 1]) --thi;//避免重复值
--thi;
}
else {
tmp.push_back(num[sec]);
tmp.push_back(num[thi]);
ret.push_back(tmp);
//target不变,继续将sec和thi往中间夹
tmp.clear();
tmp.push_back(num[i]);
while (num[sec] == num[sec+1]) ++sec;//避免重复值
++sec;
while (num[thi] == num[thi - 1]) --thi;//避免重复值
--thi;
}
}
}
return ret;
}
4sum:
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.
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]
]
解答过程采用olivier.valery的代码,如下所示,
vector<vector> fourSum(vector& nums, int target) {
std::unordered_set <std::string> myset;
vector<vector<int>> res;
int n = nums.size();
if (n<4)
return res;
sort(nums.begin(), nums.end());
for (int i = 0; i<nums.size() - 3; i++) {
int target2 = -nums[i];
for (int j = i + 1; j<nums.size() - 2; j++) {
int target3 = -nums[j];
int ptr1 = j + 1;
int ptr2 = nums.size() - 1;
while (ptr1<ptr2) {
int sum = nums[ptr1] + nums[ptr2] - target2 - target3;
if (sum == target) {
vector<int> temp;
temp.push_back(nums[i]);
temp.push_back(nums[j]);
temp.push_back(nums[ptr1]);
temp.push_back(nums[ptr2]);
string s = to_string(nums[i]) + to_string(nums[j]) + to_string(nums[ptr1]) + to_string(nums[ptr2]);
if (myset.find(s) == myset.end()) {
myset.emplace(s);
res.push_back(temp);
}
while (nums[ptr1] == temp[0])
ptr1++;
while (nums[ptr2] == temp[1])
ptr2--;
ptr1++;
}
else {
if (sum>target)
ptr2--;
else
ptr1++;
}
// Processing duplicates of Number 1
while (i + 1 < nums.size() && nums[i + 1] == nums[i])
i++;
}
}
}
return(res);
}
声明:以上题目来源于leetcode,解题思路来自网络上各位编程爱好者。