[LintCode] Subsets
Given a set of distinct integers, return all possible subsets.
Notice
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
If S = [1,2,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Can you do it in both recursively and iteratively?
Solution 1. Recursion
1 class Solution { 2 public ArrayList<ArrayList<Integer>> subsets(int[] nums) { 3 ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>(); 4 if(nums == null){ 5 return results; 6 } 7 Arrays.sort(nums); 8 getSubsets(results, new ArrayList<Integer>(), nums, 0); 9 return results; 10 } 11 12 private void getSubsets(ArrayList<ArrayList<Integer>> results, 13 ArrayList<Integer> result, 14 int[] nums, int startIdx){ 15 results.add(new ArrayList<Integer>(result)); 16 for(int i = startIdx; i < nums.length; i++){ 17 result.add(nums[i]); 18 getSubsets(results, result, nums, i + 1); 19 result.remove(result.size() - 1); 20 } 21 } 22 }
Solution 2. Iterative
Algorithm:
if there are n elements in nums[], then we'll have 2^n different subsets in total.
Each outer loop genereates a different subset. For a given i, its bits of 1 represent
that which elements should be included in the ith subset.
Each inner loop checks if nums[j] should be included in the ith subset.
For example, nums = {1, 2, 3}, i = 5 with binary representation of 101. This means
nums[0] and nums[2] should be included in the 5th subset.
Then in the inner loop checks if each nums[j] should be included in the 5th subset.
1 class Solution { 2 public ArrayList<ArrayList<Integer>> subsets(int[] nums) { 3 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); 4 int n = nums.length; 5 Arrays.sort(nums); 6 7 for (int i = 0; i < (1 << n); i++) { 8 ArrayList<Integer> subset = new ArrayList<Integer>(); 9 for (int j = 0; j < n; j++) { 10 // check whether the jth digit in i's binary representation is 1 11 if ((i & (1 << j)) != 0) { 12 subset.add(nums[j]); 13 } 14 } 15 result.add(subset); 16 } 17 18 return result; 19 } 20 }
Both solutions' runtime are O(2^n), and it is already optimal since there are 2^n different subsets to generate.
Related Problems
Subsets II
Restore IP Address
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 全程使用 AI 从 0 到 1 写了个小工具
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· AI 插件第二弹,更强更好用
· Blazor Hybrid适配到HarmonyOS系统