典型的递归状态生成问题。类似于全排列的生成问题。
问题一:无重复元素集合的子集。Given a set of distinct integers, S, return all possible subsets.
思路:借助一个now数组存储临时的子集。不断切换状态进行深层递归,在递归最深处保存生成的子集。
class Solution { public: vector<vector<int> > subsets(vector<int> &S) { vector<vector<int> > result; if(S.empty()) return result; sort(S.begin(), S.end()); vector<int> now; fun(S, result, now, 0); return result; } void fun(const vector<int> &s, vector<vector<int> > &result, vector<int> &now, int n) { if(n == s.size()) { result.push_back(now); //递归最深处:所有元素都判定过取或不取 return; } else { fun(s, result, now, n+1); //不取 now.push_back(s[n]); fun(s, result, now, n+1); //取 now.pop_back(); //回溯前一定要恢复回原样 } } };
问题二:有重复元素集合的子集。Given a collection of integers that might contain duplicates, S, return all possible subsets.
class Solution { public: vector<vector<int> > subsetsWithDup(vector<int> &S) { vector<vector<int> > result; vector<int> now; sort(S.begin(), S.end());//排序 set<vector<int> > s; backtrack(s, now, S, 0); set<vector<int> >::iterator it = s.begin(); for(;it != s.end();it++) result.push_back(*it); return result; } void backtrack(set<vector<int> > &result, vector<int> &now, vector<int> &S, int idx) { if(idx == S.size()) { result.insert(now);//set去重复 return; } backtrack(result, now, S, idx+1); now.push_back(S[idx]); backtrack(result, now, S, idx+1); now.pop_back(); } };