C++實現(xiàn)LeetCode(39.組合之和)
[LeetCode] 39. Combination Sum 組合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
像這種結果要求返回所有符合要求解的題十有八九都是要利用到遞歸,而且解題的思路都大同小異,相類似的題目有 Path Sum II,Subsets II,Permutations,Permutations II,Combinations 等等,如果仔細研究這些題目發(fā)現(xiàn)都是一個套路,都是需要另寫一個遞歸函數,這里我們新加入三個變量,start 記錄當前的遞歸到的下標,out 為一個解,res 保存所有已經得到的解,每次調用新的遞歸函數時,此時的 target 要減去當前數組的的數,具體看代碼如下:
解法一:
class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> res; vector<int> out; combinationSumDFS(candidates, target, 0, out, res); return res; } void combinationSumDFS(vector<int>& candidates, int target, int start, vector<int>& out, vector<vector<int>>& res) { if (target < 0) return; if (target == 0) {res.push_back(out); return;} for (int i = start; i < candidates.size(); ++i) { out.push_back(candidates[i]); combinationSumDFS(candidates, target - candidates[i], i, out, res); out.pop_back(); } } };
我們也可以不使用額外的函數,就在一個函數中完成遞歸,還是要先給數組排序,然后遍歷,如果當前數字大于 target,說明肯定無法組成 target,由于排過序,之后的也無法組成 target,直接 break 掉。如果當前數字正好等于 target,則當前單個數字就是一個解,組成一個數組然后放到結果 res 中。然后將當前位置之后的數組取出來,調用遞歸函數,注意此時的 target 要減去當前的數字,然后遍歷遞歸結果返回的二維數組,將當前數字加到每一個數組最前面,然后再將每個數組加入結果 res 即可,參見代碼如下:
解法二:
class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> res; sort(candidates.begin(), candidates.end()); for (int i = 0; i < candidates.size(); ++i) { if (candidates[i] > target) break; if (candidates[i] == target) {res.push_back({candidates[i]}); break;} vector<int> vec = vector<int>(candidates.begin() + i, candidates.end()); vector<vector<int>> tmp = combinationSum(vec, target - candidates[i]); for (auto a : tmp) { a.insert(a.begin(), candidates[i]); res.push_back(a); } } return res; } };
我們也可以用迭代的解法來做,建立一個三維數組 dp,這里 dp[i] 表示目標數為 i+1 的所有解法集合。這里的i就從1遍歷到 target 即可,對于每個i,都新建一個二維數組 cur,然后遍歷 candidates 數組,如果遍歷到的數字大于i,說明當前及之后的數字都無法組成i,直接 break 掉。否則如果相等,那么把當前數字自己組成一個數組,并且加到 cur 中。否則就遍歷 dp[i - candidates[j] - 1] 中的所有數組,如果當前數字大于數組的首元素,則跳過,因為結果要求是要有序的。否則就將當前數字加入數組的開頭,并且將數組放入 cur 之中即可,參見代碼如下:
解法三:
class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<vector<int>>> dp; sort(candidates.begin(), candidates.end()); for (int i = 1; i <= target; ++i) { vector<vector<int>> cur; for (int j = 0; j < candidates.size(); ++j) { if (candidates[j] > i) break; if (candidates[j] == i) {cur.push_back({candidates[j]}); break;} for (auto a : dp[i - candidates[j] - 1]) { if (candidates[j] > a[0]) continue; a.insert(a.begin(), candidates[j]); cur.push_back(a); } } dp.push_back(cur); } return dp[target - 1]; } };
到此這篇關于C++實現(xiàn)LeetCode(39.組合之和)的文章就介紹到這了,更多相關C++實現(xiàn)組合之和內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!