暴力枚舉法專題

聲明:題目均來自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;
}
發佈了70 篇原創文章 · 獲贊 267 · 訪問量 107萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章