Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
其实就是Power Set.
Solution 1:
DFS, Recursive
public List<List<Integer>> subsets(int[] S) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(S); getSubsets(result, new ArrayList<Integer>(), S, 0); return result; } private void getSubsets(List<List<Integer>> result, List<Integer> list, int[] S, int start) { result.add(new ArrayList<Integer>(list)); for(int i=start; i<S.length; i++) { list.add(S[i]); getSubsets(result, list, S, i+1); list.remove(list.size()-1); } }
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> res; vector<int> list; sort(nums.begin(), nums.end()); function<void(int)> backtrack = [&](int start) { res.push_back(list); for(int i=start; i<nums.size(); i++) { list.push_back(nums[i]); backtrack(i+1); list.pop_back(); } }; backtrack(0); return res; }
Solution 2:
BFS, Iterative.
public List<List<Integer>> subsets(int[] S) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(S); result.add(new ArrayList<Integer>()); for(int item: S) { List<List<Integer>> newResult = new ArrayList<>(result); for(List<Integer> list: result) { List<Integer> newList = new ArrayList<>(list); newList.add(item); newResult.add(newList); } result = newResult; } return result; }
Output:
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> res; res.push_back(vector<int>()); sort(nums.begin(), nums.end()); for(int num:nums) { int n = res.size(); for(int i=0; i<n; i++) { auto list = res[i]; list.push_back(num); res.push_back(list); } } return res; }
Solution 3:
Bitmap, binary representation
In S, , a 1 in the position corresponding to the location in the set indicates the presence of the element. So {x, y} = 110.
For the whole power set of S we get:
- { } = 000 (Binary) = 0 (Decimal)
- {x} = 100 = 4
- {y} = 010 = 2
- {z} = 001 = 1
- {x, y} = 110 = 6
- {x, z} = 101 = 5
- {y, z} = 011 = 3
- {x, y, z} = 111 = 7
public List<List<Integer>> subsets(int[] S) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(S); int n = S.length; int size = (int)Math.pow(2, n); for(int i=0; i<size; i++) { List<Integer> list = new ArrayList<>(); for(int j=0; j<n; j++) { if((i>>>j & 1) == 1) { list.add(S[j]); } } result.add(list); } return result; }
vector<vector<int>> subsets(vector<int>& nums) { int n = nums.size(); int size = pow(2, n); sort(nums.begin(), nums.end()); vector<vector<int>> res; for(int i=0; i<size; i++) { vector<int> v; for(int j=0; j<n; j++) { if(i&(1<<j)) v.push_back(nums[j]); } res.push_back(v); } return res; }
Time Complexity: O(n2^n)
Reference:
相关推荐
大佬的leetcode刷题笔记(c++版本)
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
LeetCode题解 - Java语言实现-181页.pdf
彩色版本 正版 pdf 精讲数据结构 + 算法 链表 树 图表 贪心算法 指针 动态规划 查找算法
leetcode1-300.docx
500195422331430LeetCode题解 - Java语言实现.zip
leetcode 答案Leetcode---数据库 我对 Leetcode 数据库问题的回答
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode 1 -200题所有源码 有问题私聊我
leetcode 答案leetcode--python Leetcode 的答案
leetcode1-240题中文题解,md格式,java版本 有题目有题解有代码 需要使用markdown打开
leetcode26 algo 算法与数据结构,练习代码 语言:java 8 开发工具:vscode,安装插件Java Extension Pack vscode有智能提示,可调试,有重构支持,满足代码练习要求,相比IDEA更轻量级,普通笔记本即可流畅运行。 ...
本书包含了 LeetCode Online Judge所有题目的答案,所有代码经过精心编写,编码规范良好,适合读者反复揣摩,模仿,甚至在纸上默写
leetcode 答案Leetcode---算法 我对 Leetcode 算法问题的回答
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 入门 先决条件 Windows 10、MacOS、Linux Chrome版 >=90.0.4430 安装 # Prepare your virtual environment conda ...