关于组合 - 给定数组,输出所有组合

Subsets
Given a set ofdistinct 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],
  []
]
2014.8.3 update:
iteration的做法:
public class Solution {
    public List> subsets(int[] S) {
        List>results = new ArrayList>();
        if(S.length == 0) {
            return results;
        }
        Arrays.sort(S);
        results.add(new ArrayList());

        for (int i= 0; i < S.length; i++) {
            List> newResults = newArrayList>();
            for (List preResult :results) {
                List newResult = new ArrayList(preResult);
                newResult.add(S[i]);
                newResults.add(newResult);
            }
            results.addAll(newResults);
        }
        returnresults;
    }
}
recursion:
    // recursion
    List> helper(int S[],int start) {
        List> result = new ArrayList>();
        if (start == S.length) {
            List list= new ArrayList();
            result.add(list);
            returnresult;
        }
         
        List> preResult = helper(S, start+1);
        result.addAll(preResult);
        for (List list : preResult) {
            ListnewList = new ArrayList(list);
            newList.add(0, S[start]);
            result.add(newList);
        }
        return result;
    }
014.8.3:

    public List>subsets(int[] S) {
        List> results = new ArrayList>();
        if (S.length == 0) {
            returnresults;
        }
        Arrays.sort(S);
        List> binaryResults =generateBinarySubsets(S.length);
         
        for (List binaryList : binaryResults) {
            ListrealList = new ArrayList();
            for (int i= 0; i < binaryList.size(); i++) {
                if(binaryList.get(i).intValue() == 1) {
                    realList.add(S[i]);
                }
            }
            results.add(realList);
        }
        return results;
    }
     
    private List>generateBinarySubsets(int n) {
        int num = (int) Math.pow(2, n) -1 ; // stupid,used 2^n here...
        List> preLists = new ArrayList>();
        List init = new ArrayList();
        preLists.add(init);
        while (num > 0) {
            List>newLists = new ArrayList>();
            for (Listlist : preLists) {
                List list0 = newArrayList(list);
                List list1 = newArrayList(list);
                list0.add(0, 0);
                list1.add(0, 1);
                newLists.add(list0);
                newLists.add(list1);
            }
            num = num>> 1;
            preLists.clear();
            preLists.addAll(newLists);
        }
        return preLists;
    }

public class Solution {
    public ArrayList>subsets(int[] S) {
        // Start typing your Java solution below
        // DO NOT write main() function
        Arrays.sort(S);
         
        return findSubsets(S, S.length-1);
    }

    ArrayList>findSubsets(int[] S, int end) {
        ArrayList> result = newArrayList>();
        if (end < 0) {
            result.add(new ArrayList());
            returnresult;
        }

        result = findSubsets(S, end-1);
        ArrayList> moreResult = newArrayList>();
        for (ArrayList lastSet : result) {
            ArrayListnewSet = new ArrayList();
            newSet.addAll(lastSet);
            newSet.add(S[end]);
            moreResult.add(newSet);
        }
        result.addAll(moreResult);
        return result;
    }
}

有重复元素:

Given a collection of integers that might containduplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be innon-descending order.
  • The solution set must notcontain duplicate subsets.

 

For example,
If S [1,2,2],a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
2014.8.4 update:
如果该数字等于前一个数字,那么只对于前一个数字已经append的list再次append
public class Solution {
    public List>subsetsWithDup(int[] num) {
        Arrays.sort(num);
        List> result = new ArrayList>();
        helper(num, 0, result);
        return result;
    }

    List> helper(int[]num, int start, List> result) {
        List> newCombinations = newLinkedList>();
        if (start == num.length) {
            List list = new LinkedList();
            newCombinations.add(list);
            result.addAll(newCombinations);
            returnresult;
        }
         
        List> preResult =helper(num, start+1, result);
       // no repeat,append currentnumber to all previous combinations
       if (start < num.length-1&& num[start] != num[start+1]) {
            for (List list : result) {
                ListnewList = new LinkedList(list);
                newList.add(0, num[start]);
                newCombinations.add(newList);
            }
        } else {
          // with repeat, only append current number tocombinations contains last same number
          for (List list : preResult) {
                ListnewList = new LinkedList(list);
                newList.add(0,num[start]);
                newCombinations.add(newList);
            }
        }
        result.addAll(newCombinations);
        return newCombinations;
    }
}

2013: wrong answer
public class Solution {
    public ArrayList>subsetsWithDup(int[] num) {
        // Note: The Solution object is instantiatedonly once and is reused by each test case.
        Arrays.sort(num);
        HashSet> result = helper(num,num.length-1);
        ArrayList> finalResult = newArrayList>();
        finalResult.addAll(result);
        return finalResult;
    }

    HashSet> helper(int[]num, int index) {
        HashSet> result = new HashSet>();
        if (index < 0) {
            result.add(new ArrayList());
            returnresult;
        }

        result = helper(num, index-1);
        ArrayList> newSets = newArrayList>();
        for (ArrayList subset : result) {
            ArrayListnewSet = new ArrayList(subset);
            newSet.add(num[index]);
            newSets.add(newSet);
        }
        result.addAll(newSets);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值