solution.md 3.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 组合
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5
<p>给定两个整数 <em>n</em><em>k</em>,返回 1 ... <em>n </em>中所有可能的 <em>k</em> 个数的组合。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre>
每日一练社区's avatar
每日一练社区 已提交
6
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
## aop
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10
### before
F
fix bug  
feilong 已提交
11

每日一练社区's avatar
每日一练社区 已提交
12
```c
每日一练社区's avatar
每日一练社区 已提交
13 14 15
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
16

每日一练社区's avatar
每日一练社区 已提交
17
### after
F
fix bug  
feilong 已提交
18

每日一练社区's avatar
每日一练社区 已提交
19
```c
每日一练社区's avatar
每日一练社区 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
int main()
{
    Solution sol;
    int n = 4;
    int k = 2;
    vector<vector<int>> res;
    res = sol.combine(n, k);
    for (auto i : res)
    {
        for (auto j : i)
            cout << j << " ";
        cout << endl;
    }
    return 0;
}

```

## 答案
F
fix bug  
feilong 已提交
39

每日一练社区's avatar
每日一练社区 已提交
40
```c
每日一练社区's avatar
每日一练社区 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class Solution
{
public:
    vector<vector<int>> combine(int n, int k)
    {
        vector<int> ones(n), lib(n);
        vector<vector<int>> result;
        for (int i = 0; i < n; i++)
        {
            ones[i] = (i < k) ? 1 : 0;
            lib[i] = i + 1;
        }
        while (1)
        {
            vector<int> solution;
            bool flag = false;
            for (int i = 0; i < n; i++)
            {
                if (ones[i])
                    solution.push_back(lib[i]);
            }
            result.push_back(solution);
            int count = 0;
            for (int i = 0; i < n - 1; i++)
            {
                if (ones[i] == 1 && ones[i + 1] == 0)
                {
                    flag = true;
                    ones[i] = 0;
                    ones[i + 1] = 1;
                    for (int j = 0; j < i; j++)
                    {
                        ones[j] = count > 0 ? 1 : 0;
                    }
                    break;
                }
                else if (ones[i])
                    count++;
            }
            if (!flag)
                break;
        }

        return result;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
90

每日一练社区's avatar
每日一练社区 已提交
91
### A
F
fix bug  
feilong 已提交
92

每日一练社区's avatar
每日一练社区 已提交
93
```c
每日一练社区's avatar
每日一练社区 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
class Solution
{
public:
    vector<vector<int>> combine(int n, int k)
    {
        vector<vector<int>> ans;
        vector<int> tmp;
        combineDFS(n, k, 1, tmp, ans);
        return ans;
    }
    void combineDFS(int n, int k, int level, vector<int> &tmp, vector<vector<int>> &ans)
    {
        if (tmp.size() == k)
        {
            ans.push_back(tmp);
            return;
        }
        for (int i = level; i <= n; ++i)
        {
            tmp.push_back(i);
            combineDFS(n, k, i + 1, tmp, ans);
            tmp.pop_back();
        }
    }
};
```

### B
F
fix bug  
feilong 已提交
122

每日一练社区's avatar
每日一练社区 已提交
123
```c
每日一练社区's avatar
每日一练社区 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
class Solution
{
public:
    vector<vector<int>> combine(int n, int k)
    {
        vector<vector<int>> result;
        int i = 0;
        vector<int> p(k, 0);
        while (i >= 0)
        {
            p[i]++;
            if (p[i] > n)
                --i;
            else if (i == k - 1)
                result.push_back(p);
            else
            {
                ++i;
                p[i] = p[i - 1];
            }
        }
        return result;
    }
};
```

### C
F
fix bug  
feilong 已提交
151

每日一练社区's avatar
每日一练社区 已提交
152
```c
每日一练社区's avatar
每日一练社区 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
class Solution
{
public:
    vector<vector<int>> res;
    vector<int> tmp;
    int n;

    void backtrack(vector<int> &tmp, int idx, int cnt, int k)
    {
        if (cnt == k)
        {
            res.push_back(tmp);
            return;
        }
        for (int i = idx + 1; i <= n; ++i)
        {
            tmp[cnt] = i;
            backtrack(tmp, i, cnt + 1, k);
            tmp[cnt] = 0;
        }
    }
    vector<vector<int>> combine(int n, int k)
    {
        this->n = n;
        tmp = vector<int>(k, 0);
        backtrack(tmp, 0, 0, k);
        return res;
    }
};
```