您的位置:首页 > 其它

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();
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: