暴力枚举法专题

声明:题目均来自Leetcode,本文旨在学习

1.Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note: 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:

//增量构造法、深搜,时间复杂度O(2^n),空间复杂度O(n)
vector<vector<int>> subsets(vector<int> &S)
{
    sort(S.begin(),S.end());
    vector<vector<int>> result;
    vector<int> path;
    subsets_core(S,path,0,result);
    return result;
}

void subsets_core(vector<int> &S,vector<int> &path, int step, vector<vector<int>> &result)
{
    if (step == S.size())
    {
        result.push_back(path);
        return;
    }

    //不选S[step]
    subsets_core(S,path,step+1,result);

    //选S[step]
    path.push_back(S[step]);
    subsets_core(S,path,step+1,result);
    path.pop_back();
}

2.Permutation

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Solution 1:

在C++中,algorithm文件提供了next_permutation这一函数的实现,这一函数在使用时,需要保证数组有序,即配合sort。

cout << "Use next_permutation function in algorithm : " << endl;
sort(vec.begin(),vec.end());
do 
{
    output_vec(vec);
} while (next_permutation(vec.begin(),vec.end()));

这一函数的基本思想如下:

(1)从尾部开始往前寻找两个相邻的元素第1个元素i,第2个元素j(从前往后数的),且i < j
(2)再从尾往前找第一个大于i的元素k。将i、k对调
(3)[j,last)范围的元素置逆(颠倒排列)

Solution 2:

全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为例说明如何编写全排列的递归算法。

1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以4开头的5的全排列和以5开头的4的全排列。由于一个数的全排列就是其本身,从而得到以上结果。

2、再看后三个数3, 4, 5。它们的全排列为3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3 六组数。

即以3开头的和4,5的全排列的组合、以4开头的和3,5的全排列的组合和以5开头的和3,4的全排列的组合.

从而可以推断,设一组数p = {r1, r2, r3, … ,rn}, 全排列为perm(p),pn = p - {rn}。

因此perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), … , rnperm(pn)。当n = 1时perm(p} = r1。

为了更容易理解,将整组数中的所有的数分别与第一个数交换,这样就总是在处理后n-1个数的全排列。

void recursive_permutation_core(vector<int>& vec, int k)
{
    if (k == vec.size())
    {
        output_vec(vec);
    } 
    else
    {
        for (int i = k; i < vec.size(); i++)
        {
            swap(&vec[k],&vec[i]);
            recursive_permutation_core(vec,k+1);
            swap(&vec[k],&vec[i]);
        }
    }
}

3.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],
]

Solution:

//深搜、递归
//时间复杂度O(n!),空间复杂度O(n)
vector<vector<int>> combine(int n, int k)
{
    vector<vector<int>> result;
    vector<int> path;
    dfs(n,k,1,0,path,result);
    return result;
}

//start 开始的数
//cur 已选择的数
void dfs(int n, int k, int start, int cur, vector<int> &path, vector<vector<int>> &result)
{
    if (cur == k)
    {
        result.push_back(path);
    }

    for (int i = start; i <= n; i++)
    {
        path.push_back(i);
        dfs(n,k,i+1,cur+1,path,result);
        path.pop_back();
    }
}

延伸,问题:从n个数中选择m个数

//start 开始的下标
//cur 已选择的数
void combine_core(vector<int>& vec, int k, int start, int cur, vector<vector<int>>& result, vector<int>& path)
{
    if (cur == k)
    {
        result.push_back(path);
    } 
    else
    {
        for (int i = start; i < vec.size(); i++)
        {
            path.push_back(vec[i]);
            combine_core(vec,k,i+1,cur+1,result,path);
            path.pop_back();
        }
    }
}

vector<vector<int>> combine(vector<int> vec, int k)
{
    vector<vector<int>> result;
    vector<int> path;
    combine_core(vec,k,0,0,result,path);
    return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值