40. Combination Sum II **
生活随笔
收集整理的這篇文章主要介紹了
40. Combination Sum II **
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
description:
給定target, 求給定數列中找到幾個數(其中的數不可以重復使用,且一組數有幾個也不做限制)的和為target,和上面那個題一毛一樣的,就改一下下標就行了,背下來背下來背下來~~~
Note:
Example:
Example 1:Input: candidates = [10,1,2,7,6,1,5], target = 8, A solution set is: [[1, 7],[1, 2, 5],[2, 6],[1, 1, 6] ]Example 2:Input: candidates = [2,5,2,1,2], target = 5, A solution set is: [[1,2,2],[5] ]answer:
class Solution { public:vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {vector<vector<int>> res;vector<int> out;sort(candidates.begin(), candidates.end()); //這里剛開始就沒寫,一定要先變成有序數組!!!combinationDFS(candidates, res, out, target, 0);return res;}void combinationDFS(vector<int>& candidates, vector<vector<int>>& res, vector<int>& out, int target, int start) {if(target < 0) return;if(target == 0) {res.push_back(out);return;}for(int i = start; i < candidates.size(); i++) {if(i > start && candidates[i] == candidates[i - 1]) continue; //這里也要注意了,因為上邊那個note的第二條out.push_back(candidates[i]);combinationDFS(candidates, res, out, target - candidates[i], i + 1);out.pop_back();}} };relative point get√:
hint :
轉載于:https://www.cnblogs.com/forPrometheus-jun/p/11166218.html
總結
以上是生活随笔為你收集整理的40. Combination Sum II **的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nodejs 单元测试
- 下一篇: WCF(五) 深入理解绑定