8.1 Subsets

Link: https://oj.leetcode.com/problems/subsets/

Approach I: Recursion 

http://blog.csdn.net/linhuanmars/article/details/24286377

注意:

1 这种解法生成解的顺序是:{{}, {1}, {2}, {1, 2}, {3}, {1, 3}, {2, 3}, {1, 2, 3}} //I can do this solution. updated @8.27.2014 

对一个长度为n 的数组S,先生成它前 S[0...n-1] 个元素的所有subsets,然后向每个subset里依次添加S[n-1].

2 必须要有:

ArrayList<Integer> elem = new ArrayList<Integer>(result.get(i));

而不能直接result.get(i).add(S[index]), 否则原来的result.get(i)也会跟着改变。

Input: [0]
Output: [[0],[0]]
Expected: [[],[0]]

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        if(S == null) return null;
        Arrays.sort(S);
        return helper(S, S.length - 1);
    }
    
    private ArrayList<ArrayList<Integer>> helper(int[] S, int index){
        if(index == -1) {
            ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> elem = new ArrayList<Integer>();
            result.add(elem);
            return result;
        }
        ArrayList<ArrayList<Integer>> result = helper(S, index-1);
        int size = result.size();//must get result's size here
        //if for(int i=0;i<result.size();i++), since result increases in each for loop
        //we will get Time limit exceeded error
        for(int i = 0; i < size; i++){
            ArrayList<Integer> elem = new ArrayList<Integer>(result.get(i));
            elem.add(S[index]);
            result.add(elem);
        }
        return result;
    }
}

DFS II: http://blog.csdn.net/u011095253/article/details/9158397

这种解法生成解的顺序是:{{}, {1}, {1, 2}, {1, 2, 3}, {1, 3}, {2}, {2, 3}, {3}}

重点记 (同样可用于Combinations)@8.29.2014

public class Solution {
     public ArrayList<ArrayList<Integer>> subsets(int[] S) {  
         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
         if(S == null || S.length == 0) return result;
         ArrayList<Integer> tmp = new ArrayList<Integer>();
         result.add(tmp);
         Arrays.sort(S);
         dfs(S, 0, tmp, result);
         return result;
     }
     
     private void dfs(int[] S, int pos, ArrayList<Integer> tmp, ArrayList<ArrayList<Integer>> result){
         //to add each element in S
         for(int i = pos; i < S.length; i++){
             tmp.add(S[i]);
             result.add(new ArrayList<Integer>(tmp));
             dfs(S, i+1, tmp, result);//i+1 not pos+1
             tmp.remove(tmp.size()-1);
         }
     }
}


Approach II: Iterative

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        //iterative
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if(S ==  null || S.length == 0) return result;
        ArrayList<Integer> list = new ArrayList<Integer>();
        result.add(list);
        Arrays.sort(S);
        for(int i = 0; i < S.length; i++){
            int size = result.size();//must record result's size first because result is increasing in each for loop
            for(int j = 0; j < size; j++){
                ArrayList<Integer> elem = new ArrayList<Integer>(result.get(j));
                elem.add(S[i]);
                result.add(elem);
            }
        }
        return result;
    }
}

Approach III: Iterative with binary number

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        //iterative with binary number
        ArrayList<ArrayList<Integer>> result = new  ArrayList<ArrayList<Integer>>();
        Arrays.sort(S);
        int size = 1<< S.length;
        for(int i = 0; i < size; i++){
            ArrayList<Integer> subset = convertIntToList(S, i);
            result.add(subset);
        }
        return result;
    }
    
    private ArrayList<Integer> convertIntToList(int[] S, int i){
        ArrayList<Integer> subset = new ArrayList<Integer>();
        int index = 0;
        for(int k = i; k > 0; k>>=1){
            if((k & 1) == 1){//must add () around k&1
                subset.add(S[index]);
            }
            index ++;
        }
        return subset;
    }
}

相关题目:

Combinations

可以看成是这道题的特殊情况。当且仅当item.size() == k时,把item加入结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值