Combination
生活随笔
收集整理的這篇文章主要介紹了
Combination
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Combination
題目:
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], ]代碼如下:
void getSub(int n, int k, int start, vector<int>& solution, vector<vector<int> >& result) {if (solution.size() == k) {result.push_back(solution);return;}for (int i = start; i <= n; i++) {solution.push_back(i);getSub(n, k, i + 1, solution, result);solution.pop_back();}}解題思路:
采用遞歸算法,用 solution 保存所需要的單個組合, result 保存所有產生的組合。
總結
以上是生活随笔為你收集整理的Combination的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bugku misc QAQ
- 下一篇: 天翼云铸牢国云安全,护航千行百业