solution.md 5.1 KB
Newer Older
1
# 组合总和
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<p>给定一个<strong>无重复元素</strong>的数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。
</p>
<p><code>candidates</code>&nbsp;中的数字可以无限制重复被选取。</p>
<p><strong>说明:</strong></p>
<ul>
    <li>所有数字(包括&nbsp;<code>target</code>)都是正整数。</li>
    <li>解集不能包含重复的组合。&nbsp;</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>candidates = [2,3,6,7], target = 7,<strong><br />输出:</strong>[[7],[2,2,3]]</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>candidates = [2,3,5], target = 8,<strong><br />输出:</strong>[[2,2,2,2],[2,3,3],[3,5]]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
    <li><code>1 &lt;= candidates.length &lt;= 30</code></li>
    <li><code>1 &lt;= candidates[i] &lt;= 200</code></li>
    <li><code>candidate</code> 中的每个元素都是独一无二的。</li>
    <li><code>1 &lt;= target &lt;= 500</code></li>
</ul>
每日一练社区's avatar
每日一练社区 已提交
23
<p>以下<font color="red">错误</font>的选项是?</p>
F
fix bug  
feilong 已提交
24

25
## aop
F
fix bug  
feilong 已提交
26

27
### before
F
fix bug  
feilong 已提交
28

29 30 31 32 33
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
```cpp
int main()
{
    Solution sol;
    vector<vector<int>> res;
    vector<int> candidates = {2, 3, 6, 7};
    int target = 7;

    res = sol.combinationSum(candidates, target);

    for (auto i : res)
    {
        for (auto j : i)
            cout << j << " ";
        cout << endl;
    }
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
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 90 91 92 93 94 95
```cpp
class Solution
{
public:
    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        set<vector<int>> res;
        vector<int> temp;
        sort(candidates.begin(), candidates.end());
        fun(candidates, target, 0, temp, res);
        vector<vector<int>> result;
        for (auto mem : res)
        {
            result.push_back(mem);
        }
        return result;
    }

    void fun(const vector<int> &candidates, int target, int index, vector<int> &temp, set<vector<int>> &res)
    {
        if (target < 0)
            return;
        if (target == 0)
        {
            res.insert(temp);
            return;
        }
        while (index < candidates.size())
        {
            temp.push_back(candidates[index]);
            fun(candidates, target - candidates[index], index + 1, temp, res);
            temp.pop_back();
            ++index;
        }
    }
};
```
## 选项

F
fix bug  
feilong 已提交
96

97
### A
F
fix bug  
feilong 已提交
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
```cpp

class Solution
{
public:
    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        vector<vector<int>> res;
        dfs(candidates, 0, target, res);
        return res;
    }

private:
    vector<int> stack;
    void dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &res)
    {
        if (target < 0)
        {
            return;
        }
        else if (target == 0)
        {
            res.push_back(stack);
        }
        else
        {
            for (int i = start; i < candidates.size(); i++)
            {
                stack.push_back(candidates[i]);
                dfs(candidates, i, target - candidates[i], res);
                stack.pop_back();
            }
        }
    }
};
```

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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
```cpp
class Solution
{
public:
    void compute(int start, int target, vector<int> &tmp, vector<int> &candidates, vector<vector<int>> &ans)
    {
        int n = candidates.size();
        for (int i = start; i < n; i++)
        {
            if (target > 0)
            {
                tmp.push_back(candidates[i]);
                compute(i, target - candidates[i], tmp, candidates, ans);
                tmp.pop_back();
            }
            else if (target < 0)
                return;
            else
            {
                ans.push_back(tmp);
                return;
            }
        }
    }

    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        vector<vector<int>> ans;
        vector<int> tmp;

        int v;

        sort(candidates.begin(), candidates.end());
        compute(0, target, tmp, candidates, ans);

        return ans;
    }
};
```

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

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
```cpp
class Solution
{
private:
    vector<vector<int>> res;
    vector<int> ans;

public:
    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        sort(candidates.begin(), candidates.end());
        int left = 0, right = 0;
        for (; right < candidates.size() && candidates[right] <= target; right++)
            ;
        backtrack(candidates, left, right == candidates.size() ? right - 1 : right, target);
        return res;
    }

private:
    void backtrack(vector<int> &candidates, int left, int right, int target)
    {
        if (target < 0)
            return;
        if (!target)
        {
            res.push_back(ans);
            return;
        }
        for (int i = left; i <= right && candidates[i] <= target; i++)
        {
            ans.push_back(candidates[i]);
            backtrack(candidates, i, right, target - candidates[i]);
            ans.pop_back();
        }
    }
};
```