从数组中选出n个数之和为k
2016-04-11 20:17
507 查看
LeetCode15. 3Sum
题目描述:
https://leetcode.com/problems/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:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
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)
LeetCode15. 3Sum 相当于1. Two Sum的一个变形嘛,题目大意就是从无序数组中找3个数a,b,c,使得a+b+c=0。返回数组中所有的可能作为结果集,结果集中不能包含重复。
思路:
我们可以写成a+b=-c的形式,-c作为target,只不过这次target不是传参指定,而是从给定的数组中找。遍历i=0到i=len-2,使得target=-nums[i],然后再用双指针的方法选数,选数的范围[i+1,len-1]。
唯一需要注意的就是去重。
代码:
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { int len=nums.size(); vector<vector<int>> res; if(len==0){ return res; } sort(nums.begin(),nums.end()); int low,high,target; for(int i=0;i<len-1;i++){ if(i>0 && nums[i-1]==nums[i]){ continue; } target=0-nums[i]; low=i+1; high=len-1; while(low<high){ if(nums[low]+nums[high]==target){ vector<int> v; v.push_back(nums[i]); v.push_back(nums[low]); v.push_back(nums[high]); res.push_back(v); low++; high--; while(low<high && nums[low-1]==nums[low]){ low++; } while(low<high && nums[high+1]==nums[high]){ high--; } } else if(nums[low]+nums[high]<target){ low++; } else{ high--; } } } return res; } };
题目2
如果是从数组中选出n个数之和为k呢,返回结果集,结果集不含重复。思路:
我们就用递归回溯来解决问题了。需要注意的还是去重。
我用pre来处理重复问题,对于递归进入的同一层函数,所选的数字不能相同,所以用pre来记录下标, 如果相同就continue.
代码:
void combinationSum(vector<vector<int>>& res, vector<int>& candidates, int len, int cur, int sum,int n, int target, vector<int>& v){ if (sum > target || cur > len || v.size()>n){ return; } else if (sum == target && v.size()==n){ res.push_back(v); return; } else{ int pre = -1; for (int i = cur+1; i < len; i++){ if (pre != -1 && candidates[pre] == candidates[i]){ continue; } pre = i; sum += candidates[i]; v.push_back(candidates[i]); combinationSum(res, candidates, len, i + 1, sum,n, target, v); v.pop_back(); sum -= candidates[i]; } } } vector<vector<int>> combinationSum(vector<int>& candidates, int n, int target){ int len = candidates.size(); vector<vector<int>> res; if (len == 0){ return res; } vector<int> v; sort(candidates.begin(),candidates.end()); combinationSum(res, candidates, len, -1, 0,n, target, v); return res; } int main() { vector<int> candidates; candidates.push_back(2); candidates.push_back(1); candidates.push_back(0); candidates.push_back(7); candidates.push_back(1); candidates.push_back(0); candidates.push_back(7); vector<vector<int>> res = combinationSum(candidates,2,9); system("pause"); return 0; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解