组合与全排列

Mar 27, 2016


[TOC]

组合

DFS求解

//求组合C(n,k),用DFS,从集合[1....n-1]中任选k个数
vector<vector<int>> combine(int n, int k)
{
    vector<vector<int>> result;
    if (n < k) return result;
    vector<int> path;
    combine_help(result, n, 1, 0, path, k);
    return result;
}
//dfs
void combine_help(vector<vector<int>> &result, int n, int k, int length, vector<int> &path, int target)
{
    if (k <= n+1 && length == target)
    {
        result.push_back(path);
        return;
    }
    if (k > n) return;
    //选
    path.push_back(k);
    combine_help(result, n, k + 1, length + 1, path, target);
    path.pop_back();
    //不选
    combine_help(result, n, k + 1, length, path, target);
}

全排列

STLnext_permutation或者prev_permutation求解

vector<vector<int>> permutation(vector<int> nums)
{
    vector<vector<int>> ret;
    sort(nums.begin(),nums.end());
    ret.push_back(nums);
    while(next_permutation(nums.begin(),nums.end());
    {
        ret.push_back(nums);
    }
    return ret;
}

上一篇博客:STL必会
下一篇博客:用动态规划求解字符串常见问题