LeetCode Two Sum、3Sum、3Sum Closest、4Sum
2018-03-23 16:30
411 查看
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].
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { map<int,int>mp; vector<int>res; int len = nums.size(); for(int i=0;i<len;i++) mp[nums[i]]=i; for(int i=0;i<len;i++) { if(mp[target-nums[i]]) { res.push_back(i); res.push_back(mp[target-nums[i]]); return res; } } } };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]
]
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { int len=nums.size(); vector<vector<int> > res; if(len<=2) return res; sort(nums.begin(),nums.end()); for(int i=0;i<len;) { int st=i+1,ed=len-1; while(st<ed) { if(nums[i]+nums[st]+nums[ed]==0) { res.push_back({nums[i],nums[st],nums[ed]}); st++; ed--; while(st<ed&&nums[st]==nums[st-1]) st++; while(st<ed&&nums[ed]==nums[ed+1]) ed--; } else if(nums[i]+nums[st]+nums[ed]<0) { st++; while(st<ed&&nums[st]==nums[st-1]) st++; } else if(nums[i]+nums[st]+nums[ed]>0) { ed--; while(st<ed&&nums[ed]==nums[ed+1]) ed--; } } i++; while((i<len)&&(nums[i]==nums[i-1])) i++; } return res; } };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 threeSumClosest(vector<int>& nums, int target) { int len=nums.size(); sort(nums.begin(),nums.end()); int res=nums[0]+nums[1]+nums[2]; if(len==3) return res; for(int i=0;i<len-2;i++) { int st=i+1,ed=len-1; while(st<ed) { int sum=nums[i]+nums[st]+nums[ed]; if(abs(sum-target)<abs(res-target)) { res=sum; if(res==target) return res; } if(sum>target) ed--; else st++; } } return res; } };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>> res; int len=nums.size(); if(len<4) return res; sort(nums.begin(),nums.end()); for(int i=0;i<len-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[len-1]+nums[len-2]+nums[len-3]<target) continue; for(int j=i+1;j<len-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[len-1]+nums[len-2]<target) continue; int st=j+1,ed=len-1; while(st<ed) { int sum=nums[i]+nums[j]+nums[st]+nums[ed]; if(sum==target) { res.push_back({nums[i],nums[j],nums[st],nums[ed]}); ed--; while(st<ed&&nums[ed]==nums[ed+1]) ed--; st++; while(st<ed&&nums[st]==nums[st-1]) st++; } else if(sum>target) { ed--; while(st<ed&&nums[ed]==nums[ed+1]) ed--; } else if(sum<target) { st++; while(st<ed&&nums[st]==nums[st-1]) st++; } } } } return res; } };
相关文章推荐
- Leetcode 1. Two Sum & 15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
- leetcode做题总结,题目3Sum,4Sum,3Sum Closest---- 2012/01/16-17-26
- LeetCode:3Sum, 3Sum Closest, 4Sum
- Python 刷题日记:LeetCode: 1&15&16-Two Sum and 3Sum
- LeetCode 2Sum, 3Sum, 4Sum
- Leetcode 2SUM-3SUM-4SUM
- leetcode--sum集合:2sum,3sum,4sum
- [LeetCode] K sum(2Sum、3Sum、4Sum)
- leetcode: 哈希——two-sum,3sum,4sum
- [leetcode] 1. twoSum; 167. Two Sum II - Input array is sorted; 15. threeSum; 16. 3Sum Closest; 18. 4
- 第五周:[leetcode] Two Sum、3Sum、3Sum Closest、4Sum
- Two Sum & 3Sum & 3Sum Closest & 4Sum
- Leetcode-2sum,3sum,4sum
- LeetCode:3Sum, 3Sum Closest, 4Sum
- leetcode 3Sum 3Sum Closest 4Sum
- [leetcode] 求和问题总结(2Sum, 3Sum, 4Sum, K Sum)
- 【leetcode74】Sum of Two Integers(不用+,-求两数之和)
- Leetcode 712 Minimum ASCII Delete Sum for Two Strings
- [LeetCode]Two Sum III - Data structure design