Q9.4 Write a method that returns all subsets of a set.
来源:互联网 发布:树熊网络wifi如何登录 编辑:程序博客网 时间:2024/05/29 15:07
Q:Write a method that returns all subsets of a set.
A:DFS.
#include <iostream>#include <vector>#include <algorithm> using namespace std;void dfs(const vector<int> &s, int start, vector<int> &path, vector<vector<int> > &res) {if (start == s.size()) {res.push_back(path);return ;}for (int i = start; i < s.size(); i++) {path.push_back(s[i]);dfs(s, i+1, path, res);path.pop_back();}res.push_back(path);return ;}vector<vector<int> > subsets(vector<int> &A) {vector<vector<int> > res;if (A.size() == 0) {return res;}vector<int> cur;dfs(A, 0, cur, res);return res;}void printResult(vector<vector<int> > &res) {for (int i = 0; i < res.size(); i++) {cout<<"[";for (int j = 0; j <res[i].size(); j++) {cout<<res[i][j];if (j != res[i].size()-1) {cout<<",";}}cout<<"]"<<endl;}}void dfs2(const vector<int> &s, int start, vector<int> &path, vector<vector<int> > &res) { if (start == s.size()) { res.push_back(path); return ; } int prev = s[start]+1; for (int i = start; i < s.size(); i++) { if (prev == s[i]) { continue; } prev = s[i]; path.push_back(s[i]); dfs2(s, i+1, path, res); path.pop_back(); } res.push_back(path); return ;}//如果存在重复元素 vector<vector<int> > subsetsWithDup(vector<int> &S) { sort(S.begin(), S.end()); vector<vector<int> > res; vector<int> path; dfs2(S, 0, path, res); return res;} int main() {vector<int> A;for (int i = 1; i <= 3; i++) {A.push_back(i);}vector<vector<int> > res = subsets(A);printResult(res);cout<<endl;A[2] = 2;res = subsetsWithDup(A);printResult(res);return 0;}
0 0
- Q9.4 Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set
- Algorithm_Write a method that returns all subsets of a set.
- Q9.5 Write a method to compute all permutations of a string
- Write a method to compute all permutations of a string
- Ch8.3: find all the subsets of a set
- Write a method that counts the number of occurrences of a given character in a string
- CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
- LeetCode Online Judge:Given a set of distinct integers, S, return all possible subsets.
- Write a function that takes a string as input and returns the string reversed.
- Q1.4 Write a method to replace all spaces in a string with ‘%20’.
- 输出一个集合所有子集的元素和(Print sums of all subsets of a given set)
- Print all possible strings of length k that can be formed from a set of n characters
- P27 (**) Group the elements of a set into disjoint subsets.
- Write a method to replace all spaces in a string with ‘%20’.
- Write a method to replace all spaces in a string with ‘%20’
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- Android开发--倒计时的实现
- EJB3.x:部署(卸载)EntityBean时自动创建(删除)表结构
- struts2的ActionEventListener
- JavaScript 对象的创建
- ListView的新朋友-------RecyclerView初识
- Q9.4 Write a method that returns all subsets of a set.
- libcurl 使用及例程
- 数组名和数组名取地址的区别
- C#中转义符和@符号的作用
- cygwin下安装mbsystem-5.4.2209 configure部分
- HDU 5105 Math Problem(数学)
- libcurl教程
- Jenkins—Jenkins是什么
- CMake命令之function