leetcode Sum系列
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> m;
vector<int> res;
if(nums.size()<2)return res;
for(int i=0;i<nums.size();++i){
int num=target-nums[i];
if(m.find(num)!=m.end()){
res.push_back(m[num]);
res.push_back(i);
return res;
}
m[nums[i]]=i;
}
return res;
}
};
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.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
Solutionvector<vector<int>> threeSum(vector<int>& num) {
vector<vector<int> > res;
std::sort(num.begin(), num.end());
for (int i = 0; i < num.size(); i++) {
int target = -num[i];
int front = i + 1;
int back = num.size() - 1;
while (front < back) {
int sum = num[front] + num[back];
if (sum < target)
front++;
else if (sum > target)
back--;
else {
vector<int> triplet(3, 0);
triplet[0] = num[i];
triplet[1] = num[front];
triplet[2] = num[back];
res.push_back(triplet);
while (front < back && num[front] == triplet[1]) front++;
while (front < back && num[back] == triplet[2]) back--;
}
}
while (i + 1 < num.size() && num[i + 1] == num[i])
i++;
}
return res;
}
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.
Example:
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]
]
Solution
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
if(nums.size()<4) return res;
vector<int>tmp(4,0);
sort(nums.begin(),nums.end());
int front,back;
for(int i=0;i<nums.size()-3;++i){
tmp[0]=nums[i];
for(int j=i+1;j<nums.size()-2;++j){
tmp[1]=nums[j];
front=j+1;
back=nums.size()-1;
int subtar=target-nums[i]-nums[j];
while(front<back){
if((nums[front]+nums[back])<subtar) ++front;
else if((nums[front]+nums[back])>subtar) --back;
else{
tmp[2]=nums[front];
tmp[3]=nums[back];
res.push_back(tmp);
while(nums[front]==tmp[2]) ++front;
while(nums[back]==tmp[3]) --back;
}
}
while(nums[j]==tmp[1]) ++j;
j--;
}
while(nums[i]==tmp[0])++i;
i--;
}
return res;
}
相關文章
- Leetcode Path SumLeetCode
- Leetcode Two SumLeetCode
- leetcode15&16_3Sum&4SumLeetCode
- Leetcode 1 two sumLeetCode
- Leetcode 3SumLeetCode
- Leetcode 4SumLeetCode
- Leetcode Path Sum IILeetCode
- Leetcode-Path SumLeetCode
- Leetcode-Two SumLeetCode
- Leetcode-3SumLeetCode
- Path Sum leetcode javaLeetCodeJava
- Two Sum leetcode javaLeetCodeJava
- 3 Sum leetcode javaLeetCodeJava
- 4 Sum leetcode javaLeetCodeJava
- LeetCode | 1 Two SumLeetCode
- [LeetCode]1.Two SumLeetCode
- Leetcode 15 3SumLeetCode
- Leetcode 18 4SumLeetCode
- LeetCode----1. Two SumLeetCode
- Leetcode - Partition Equal Subset SumLeetCode
- LeetCode-3Sum SmallerLeetCode
- LeetCode-Count of Range SumLeetCode
- LeetCode1:Two SumLeetCode
- Leetcode 3Sum ClosestLeetCode
- Leetcode Sum Root to Leaf NumbersLeetCode
- Leetcode Minimum Path SumLeetCode
- Leetcode-Path Sum IILeetCode
- Leetcode-3Sum ClosestLeetCode
- Path Sum II leetcode javaLeetCodeJava
- [LeetCode]1. Two SumLeetCode
- LeetCode OJ : 1 Two SumLeetCode
- LeetCode-1 Two SumLeetCode
- LeetCode 259. Three Sum SmallerLeetCode
- LeetCode: Two sum(兩數之和)LeetCode
- Leetcode 16 3Sum ClosestLeetCode
- python: leetcode - 1 Two SumPythonLeetCode
- LeetCode Problem-Sum of Two IntegersLeetCode
- [leetCode][013] Two Sum 2LeetCode