#分治算法两小题
算法概论第二周
题目链接
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
public:
vector<vector<int>> result;
vector<vector<int>> permute(vector<int>& nums) {
//特殊条件,长度为0或1
if(nums.size() == 0 || nums.size() == 1){
result.push_back(nums);
}
else{
dfs(nums, 0, nums.size());
}
return result;
}
void dfs(vector<int>& nums, int sid, int len){
//递归终止条件,枚举串已经生成
if(sid == len - 1){
vector<int> res = nums;
result.push_back(res);
}
//枚举剩余数字交换的所有情况
else{
for(int i = sid; i < len; i++){
swap(nums, sid, i);
dfs(nums, sid + 1, len);
swap(nums, sid, i);
}
}
}
//辅助函数
vector<int> swap(vector<int>& nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
return nums;
}
};
题目链接
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
Example:
Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
###思路分析
###代码实现和解析
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
//处理特殊情况
if(n == 0){
vector<TreeNode*> res;
return res;
}
return dc(1, n);
}
//分治函数,start表示起始数字,num为树节点数目
vector<TreeNode*> dc(int start, int num){
//用于储存所有可能
vector<TreeNode*> res;
//数目为0,表示此子树为空
if(num == 0){
res.push_back(NULL);
}
//数目为一,直接插节点
else if(num == 1){
TreeNode* node = new TreeNode(start);
res.push_back(node);
}
else{
for(int i = 0; i < num; i++){
//分别处理左右子树,但要注意保证:
//左子树比右子树小
//两树节点总和正确
vector<TreeNode*> left = dc(start, i);
vector<TreeNode*> right = dc(start + i + 1, num - i - 1);
int llen = left.size();
int rlen = right.size();
for(int k = 0; k < llen; k++){
for(int j = 0; j < rlen; j++){
//尝试组合左右所有可能
TreeNode* root = new TreeNode(i + start);
root->left = left[k];
root->right = right[j];
res.push_back(root);
}
}
}
}
return res;
}
};