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 | 1 Two SumLeetCode
- Leetcode 39 Combination SumLeetCode
- Leetcode 1 two sumLeetCode
- LeetCode-1 Two SumLeetCode
- LeetCode 112. Path SumLeetCode
- python: leetcode - 1 Two SumPythonLeetCode
- leetcode-39-Combination SumLeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 15 3SumLeetCode
- Leetcode 18 4SumLeetCode
- [LeetCode]1.Two SumLeetCode
- LeetCode 259. Three Sum SmallerLeetCode
- LeetCode: Two sum(兩數之和)LeetCode
- [LeetCode] 523. Continuous Subarray SumLeetCode
- [LintCode/LeetCode] Check Sum of K PrimesLeetCode
- Leetcode 16 3Sum ClosestLeetCode
- LeetCode之Sum of Left Leaves(Kotlin)LeetCodeKotlin
- leetcode 371. Sum of Two IntegersLeetCode
- LeetCode Two Sum(001)解法總結LeetCode
- LeetCode: 560. Subarray Sum Equals KLeetCode
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- LeetCode 129. Sum Root to Leaf NumbersLeetCode
- [LeetCode] 560. Subarray Sum Equals KLeetCode
- [LeetCode] 416. Partition Equal Subset SumLeetCode
- LeetCode #1:Two Sum(簡單題)LeetCode
- [LeetCode] 2841. Maximum Sum of Almost Unique SubarrayLeetCode
- LeetCode 3Sum(015)解法總結LeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- python leetcode 之兩數之和(two sum)PythonLeetCode
- LeetCode之Sum of Even Numbers After Queries(Kotlin)LeetCodeKotlin
- LeetCode 3Sum Closest(016)解法總結LeetCode
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- 18. 4Sum(Leetcode每日一題-2020.10.05)LeetCode每日一題
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- LeetCode 之 JavaScript 解答第一題 —— 兩數之和(Two Sum)LeetCodeJavaScript