BackTracking
2016-05-18 11:10
429 查看
78. Subsets
Problem
Given a set of distinct integers, nums, return all possible subsets.Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Solution
class Solution { public: std::vector<std::vector<int> > subsets(std::vector<int> &nums) { std::sort(nums.begin(), nums.end()); std::vector<std::vector<int> > res; std::vector<int> vec; subsets(res, nums, vec, 0); return res; } private: void subsets(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) { res.push_back(vec); for (int i = begin; i != nums.size(); ++i) { vec.push_back(nums[i]); subsets(res, nums, vec, i + 1); vec.pop_back(); } } };
90. Subsets II
Problem
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example, If nums = [1,2,2], a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
Solution
class Solution { public: std::vector<std::vector<int> > subsetsWithDup(std::vector<int> &nums) { std::sort(nums.begin(), nums.end()); std::vector<std::vector<int> > res; std::vector<int> vec; subsetsWithDup(res, nums, vec, 0); return res; } private: void subsetsWithDup(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) { res.push_back(vec); for (int i = begin; i != nums.size(); ++i) if (i == begin || nums[i] != nums[i - 1]) { vec.push_back(nums[i]); subsetsWithDup(res, nums, vec, i + 1); vec.pop_back(); } } };
77. Combinations
Problem
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], ]
Solution
class Solution { public: vector<vector<int>> combine(int n, int k) { vector<int> cur; vector<vector<int>> ret; combineCore(cur,ret,0,n,k); return ret; } void combineCore(vector<int>& cur,vector<vector<int>>& ret,int start,int n,int k) { if(cur.size() == k) { sort(cur.begin(),cur.end()); ret.push_back(cur); return; } for(int i = start;i<n;++i) { cur.push_back(i+1); combineCore(cur,ret,i+1,n,k); cur.pop_back(); } } };
39. Combination Sum
Problem
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]
Solution
class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { std::sort(candidates.begin(), candidates.end()); std::vector<std::vector<int> > res; std::vector<int> combination; combinationSumCore(combination,res,candidates, target,0); return res; } void combinationSumCore(vector<int>& cur,vector<vector<int>>& ret,vector<int>& candidates, int target,int begin) { if(target == 0) { ret.push_back(cur); return; } for(int i = begin;i<candidates.size()&& candidates[i] <= target;++i) { cur.push_back(candidates[i]); combinationSumCore(cur,ret,candidates,target-candidates[i],i); cur.pop_back(); } } };
40. Combination Sum II
Problem
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]
Solution
class Solution { public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { std::sort(candidates.begin(), candidates.end()); std::vector<std::vector<int> > ret; std::vector<int> cur; combinationSum2Core(candidates, target, cur, ret, 0); return ret; } void combinationSum2Core(vector<int>& candidates, int target,vector<int>& cur,vector<vector<int>>& ret,int begin) { if(target == 0) { ret.push_back(cur); return; } for(int i = begin;i<candidates.size() && candidates[i] <= target;++i) { if (i == begin || candidates[i] != candidates[i - 1]) { cur.push_back(candidates[i]); combinationSum2Core(candidates, target - candidates[i], cur, ret, i + 1); cur.pop_back(); } } } };
216. Combination Sum III
Problem
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.Ensure that numbers within the set are sorted in ascending order.
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]]
Solution
class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<int> cur; vector<vector<int>> ret; combinationSum3Core(cur,ret,k,n,0); return ret; } void combinationSum3Core(vector<int>& cur,vector<vector<int>>& ret,int k,int n,int begin) { if(cur.size() == k && n == 0) { ret.push_back(cur); return; } for(int i = begin;i<9 && i+1 <= n;++i) { cur.push_back(i+1); combinationSum3Core(cur,ret,k,n-i-1,i+1); cur.pop_back(); } } };
相关文章推荐
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- 消息模型
- perl 使用SUPER类来访问覆盖的方法
- Android Gradle配置Debug和release参数的方法
- 计算机网络常识(摘抄)
- oracle显示中文乱码
- 平安好医生技术栈的分析【转】
- 完整简单的红黑树算法
- Soft-Margin SVM
- 光纤收发器TR-962D/932D的面板指示灯及开关代表的含义?
- OracleINSERT提示IGNORE_ROW_ON_DUPKEY_INDEX
- Scrum 项目4.0--软件工程
- Android-Volley详解
- perl 使用SUPER类来访问覆盖的方法
- iPhone 应用开发的5个贴士
- 互联网运维工作的三个层次
- 为什么天朝互联网的三巨头是BAT
- fetch="select" 与lazy="false"
- CS231n笔记7-ConvNets Typical Architecture与VGGNet
- Python函数的各种参数用法(含星号参数)