[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.
Example

If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
Challenge 

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

 

posted @   Review->Improve  阅读(235)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 全程使用 AI 从 0 到 1 写了个小工具
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· AI 插件第二弹,更强更好用
· Blazor Hybrid适配到HarmonyOS系统
点击右上角即可分享
微信分享提示