- 題目一:
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], ]
給你兩個整數 n和k,從1-n中選擇k個數字的組合。比如n=4,那么從1,2,3,4中選取兩個數字的組合
- 解題思路:http://www.cnblogs.com/Kobe10/p/6361336.html
- 題目二:
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak). The solution set must not contain duplicate combinations. For example, given candidate set2,3,6,7and target7, A solution set is: [7] [2, 2, 3]
給你一個正數數組candidate[],一個目標值target,尋找里面所有的不重復組合,讓其和等於target,給你[2,3,6,7] 2+2+3=7 ,7=7,所以可能組合為[2,2,3],[7])
- 解題思路:http://www.cnblogs.com/Kobe10/p/6361336.html
- 題目三:
給定候選數字(C)和目標數字(T)的集合,找到C中的所有唯一組合,其中候選數字總計為T. C中的每個數字只能在組合中使用一次。 注意: 所有數字(包括目標)將是正整數。 組合(a1,a2,...,ak)中的元素必須以非降序排列。 (即,a 1≤a2≤...≤ak)。 解集必須不包含重復組合。 例如,給定候選集10,1,2,7,6,1,5和target8, 解決方案集是: [1,7] [1,2,5] [2,6] [1,1,6]
- 思路:這個題目和第二題是一個類型的題目,但是他要求的是出現的組合中不能出現重復的數字。所以就要設置一個剪枝函數來限制出現重復的元素,對每一個進入的元素進行判斷,看當前元素是否等於上一個元素(因為是遞增的關系,所以只要比較上一個元素即可),如果等於就跳過這個元素,繼續進行運算。
- 代碼
class Solution { public: vector<vector<int> > res; vector<vector<int> > combinationSum2(vector<int> &num, int target) { vector<vector<int> > res; sort(num.begin(),num.end()); vector<int> temp; backtrack(res,num,temp,target,0); return res; } void backtrack(vector<vector<int> > &res,vector<int> &candidates, vector<int> &temp,int target,int start){ if (target < 0) return; else if (target == 0){ res.push_back(temp); return ; } for(int i=start;i<candidates.size();i++){ if(i>start && candidates[i]==candidates[i-1])continue;//i>start表示第一次不需要進行判斷,之后再進行判斷 temp.push_back(candidates[i]); backtrack(res,candidates,temp,target-candidates[i],i+1); temp.pop_back(); } } };
- 題目四:
給定一組不同的整數S,返回所有可能的子集。 注意: 子集中的元素必須以非降序排列。 解集必須不包含重復的子集。 例如, 如果S = [1,2,3],解決方案是: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]]
-
思路:這個題目和第一個題目有點類似。這個題目是數組的所有的組合,而第一題是指定個數的組合數。所以這個是輸出所有的,在第一題目的基礎上循環輸出給定數目的組合數。
-
代碼:
class Solution { public: //這道題也是回溯法的套路啊,題目類似於Combinations啊,但是這個是所有的組合,而那個是指定個數的組合,所以只要循環的進行 //回溯就可以實現所有的組合 vector<vector<int> > res; vector<vector<int> > subsets(vector<int> &S) { vector<int> temp; sort(S.begin(), S.end()); for (int i=0; i<=S.size(); i++) backtrack(S, temp, 0, i); return res; } void backtrack(vector<int> &S, vector<int> temp, int start, int k){ if (k < 0) return ; else if (k == 0) res.push_back(temp); else{ for (int i=start; i<S.size(); i++){ temp.push_back(S[i]); backtrack(S, temp, i+1, k-1); temp.pop_back(); } } } };
- 題目五:
給定一個可能包含重復S的整數集合,返回所有可能的子集。 注意: 子集中的元素必須按照降序排列。 解集必須不包含重復的子集。 例如, 如果S = [1,2,2],解決方案是: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]]
- 思路:這里加上了重復的元素,所以直接處理看前后兩個元素是否相等。如果相等就跳過這個元素。
- 代碼:
class Solution { public: vector<vector<int> > res; vector<vector<int> > subsetsWithDup(vector<int> &S) { vector<int> temp; sort(S.begin(), S.end()); for (int i=0; i<=S.size(); i++) backtrack(S, temp, 0, i); return res; } void backtrack(vector<int> &S, vector<int> temp, int start, int k){ if (k < 0) return ; else if (k == 0) res.push_back(temp); else{ for (int i=start; i<S.size(); i++){ if (i>start && S[i] == S[i-1]) continue; temp.push_back(S[i]); backtrack(S, temp, i+1, k-1); temp.pop_back(); } } } };