分治算法两小题

#分治算法两小题
算法概论第二周

46. Permutations

题目链接

题目描述

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]
]

思路分析

  • 每次变换一对数字
  • 使用DFS实现

代码实现和解析

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;
    }
};

95. Unique Binary Search Trees II

题目链接

题目描述

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值