LeetCode: Combination Sum系列
2016-07-27 16:02
323 查看
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
回溯
class Solution { public: void tocom(vector<vector<int>> &res,int beg,int end,int k,vector<int> &v) { if(!k) { res.push_back(v); return; } for(int i=beg;i<=end;++i) { vector<int> v1{v}; v1.push_back(i); tocom(res,i+1,end,k-1,v1); } } vector<vector<int>> combine(int n, int k) { vector<vector<int>> res; vector<int> v{}; tocom(res,1,n,k,v); return res; } };
39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
class Solution { public: void search(vector<int>& candidates,vector<vector<int>> &res,int &i,int &target,int &sz,vector<int> v0) { for(int j=i;j<sz;++j) { int m=target-candidates[j]; if(target>candidates[j]) { if(m>=candidates[i]) { vector<int> v1{v0}; v1.push_back(candidates[j]); search(candidates,res,j,m,sz,v1); } } else if(!m) { vector<int> v1{v0}; v1.push_back(candidates[j]); res.push_back(v1);//出现能够符合条件的加入结果 return; } else { return; } } } //总结:不要随意在结果集里面添加元素,想着以后再删,这样只会拉长时间,不如只添加合乎要求的。 vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> res; sort(candidates.begin(),candidates.end());//可以及时停止循环 int sz=candidates.size(); for(int i=0;i<sz;++i) { int m=target-candidates[i]; if(target>candidates[i]) { if(m>=candidates[i]) { search(candidates,res,i,m,sz,{candidates[i]}); } } else if(!m) { res.push_back({candidates[i]}); } else { break; } } return res; } };
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
res.assign(sv.begin(),sv.end());//仅顺序容器定义了assign(),这里学会了这个成员的用法。注意array没有定义。
class Solution { public: void search(vector<int>& candidates,vector<vector<int>> &res,int i,int &target,int &sz,vector<int> v0) { for(int j=i;j<sz;++j) { int m=target-candidates[j]; if(target>candidates[j]) { if(m>=candidates[i]) { vector<int> v1{v0}; v1.push_back(candidates[j]); search(candidates,res,j+1,m,sz,v1); } } else if(!m) { vector<int> v1{v0}; v1.push_back(candidates[j]); res.push_back(v1);//出现能够符合条件的加入结果 return; } else { return; } } } vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> res; sort(candidates.begin(),candidates.end());//可以及时停止循环 int sz=candidates.size(); for(int i=0;i<sz;++i) { int m=target-candidates[i]; if(target>candidates[i]) { if(m>=candidates[i]) { search(candidates,res,i+1,m,sz,{candidates[i]}); } } else if(!m) { res.push_back({candidates[i]}); } else { break; } } set<vector<int>> sv(res.begin(),res.end());//去重 res.clear(); res.assign(sv.begin(),sv.end());//仅顺序容器定义了assign() return res; } };
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
class Solution { public: void search(vector<vector<int>> &res,int beg,int k,int n,vector<int> v) { if((!k)&&(!n)) { res.push_back(v); return; } for(int i=beg+1;i<=9&&i<=n;++i) { vector<int> v1{v}; v1.push_back(i); search(res,i,k-1,n-i,v1); } } vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int>> res; vector<int> v{}; search(res,0,k,n,v); return res; } };
377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:
nums = [1, 2, 3]
target = 4
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Therefore the output is 7.
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
回溯超时,对我而言里面最难的一个,动态规划,其实我想到了利用target以前数的状态,但是还是不知道如何写,后来参考别人代码写出。
https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution
class Solution { public: // int search(vector<int>& nums,int sz, int target) // { // if(!target) return 1; // if(target<0) return 0; // int res{0}; // for(int i=0;i<sz&&nums[i]<=target;++i) // { // res+=search(nums,sz,target-nums[i]); // } // return res; // } int combinationSum4(vector<int>& nums, int target) { sort(nums.begin(),nums.end()); vector<int> res(target+1,0); for(int i=1;i<=target;++i) { for(const int &im:nums) { if(im>i) { break; } else if(i==im) { res[i]+=1;//可能在此之前就已经有值 } else { res[i]+=res[i-im];//利用前面已求状态得到现在状态。 } } } return res[target]; } };
相关文章推荐
- 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 题解