class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> rst;
unordered_map<int,int> hash;
for(int i=0;i<nums.size();i++)
{
int n=target-nums[i];
if(hash.find(n)!=hash.end())
{
rst.push_back(hash[target-nums[i]]);
rst.push_back(i);
return rst;
}else hash[nums[i]]=i;
}
return rst;
}
};
2. 3Sum
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.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]这题结果可能有重复,并且不要求输出下标,所以可以先排序,再每次固定第一个数,对后两个数用two pointer。front back
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> rst;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++)
{
if(i>0&&nums[i]==nums[i-1]) continue; //跳过跟n1重复的
int n1=nums[i];
int front=i+1;
int back=nums.size()-1;
while(front<back)
{
n2=nums[front];
n3=nums[back];
if(n1+n2+n3<0) front++;
else if(n1+n2+n3>0) back--;
else{
vector<int> tmp{n1,n2,n3};
rst.push_back(tmp);
while(front<back&&nums[front]==n2) front++; //跳过跟n2重复的
while(front<back&&nums[back]==n3) back--; //跳过跟n3重复的
}
}
}
return rst;
}
};
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).这题跟上题类似,不过答案只有一个,且每次要判断是不是最近的点,求什么设什么。
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int rst=0,len=A.size();
unordered_map<int,int> hasha,hashb,hashc,hashd;
sort(A.begin(),A.end());
sort(B.begin(),B.end());
sort(C.begin(),C.end());
sort(D.begin(),D.end());
inithash(A,hasha);
inithash(B,hashb);
inithash(C,hashc);
inithash(D,hashd);
if(A[0]+B[0]+C[0]+D[0]>0) return 0;
if(A[len-1]+B[len-1]+C[len-1]+D[len-1]<0) return 0;
for(int i=0;i<len;i++)
{
if(i>0&&A[i]==A[i-1]) continue;
int sum3=0-A[i];
for(int j=0;j<len;j++){
if(j>0&&A[j]==A[j-1]) continue;
if(B[j]+C[j]+D[j]>0) break;
if(B[len-1]+B[len-1]+C[len-1]<0)
}
}
}
void inithash(vector<int>&A, unordered_map<int,int> &hash){
for(int i=0;i<A.size();i++){
if(hash.find(a)==hash.end()) hash[a]=1;
else hash[a]++;
}
}
};
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S 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.
For example, given array S = [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] ]
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> total;
int n = nums.size();
if(n<4) return total;
sort(nums.begin(),nums.end());
for(int i=0;i<n-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[n-3]+nums[n-2]+nums[n-1]<target) continue;
for(int j=i+1;j<n-2;j++)
{
if(j>i+1&&nums[j]==nums[j-1]) continue;
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[n-2]+nums[n-1]<target) continue;
int left=j+1,right=n-1;
while(left<right){
int sum=nums[left]+nums[right]+nums[i]+nums[j];
if(sum<target) left++;
else if(sum>target) right--;
else{
total.push_back(vector<int>{nums[i],nums[j],nums[left],nums[right]});
do{left++;}while(nums[left]==nums[left-1]&&left<right);
do{right--;}while(nums[right]==nums[right+1]&&left<right);
}
}
}
}
return total;
}
};
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l)
there are such that A[i] + B[j] + C[k] + D[l]
is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
class Solution {
public:
void fillMap(vector<int>& A, vector<int>& B, unordered_map<int,int> &m)
{
int n = A.size();
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
++m[A[i] + B[j]];
}
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> m1, m2;
fillMap(A, B, m1);
fillMap(C, D, m2);
int res = 0;
for(auto it = m1.begin(); it != m1.end(); ++it)
{
auto it2 = m2.find(-it->first);
if(it2 != m2.end())
res += it->second*it2->second;
}
return res;
}