标签:des style blog io color os ar for sp
Given a set of distinct integers, S, return all possible subsets.
Note:
For example,
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
思路是,每次添加一个元素,就是在上一次的基础上,对每个列表加入元素,再与上一次的合并。
1 public class Solution { 2 public List<List<Integer>> subsets(int[] S) { 3 List<List<Integer>> lastList=new ArrayList<>(); 4 List<Integer> nonSet= new ArrayList<>(); 5 lastList.add(nonSet); 6 if (S==null || S.length==0) { 7 return lastList; 8 } 9 Arrays.sort(S); 10 11 for (int i = 0; i < S.length; i++) { 12 List<List<Integer>> tempList=new ArrayList<>(); 13 for (List<Integer> list : lastList) { 14 List<Integer> currlist=new ArrayList<>(); 15 currlist.addAll(list); 16 currlist.add(S[i]); 17 tempList.add(currlist); 18 } 19 lastList.addAll(tempList); 20 } 21 return lastList; 22 } 23 }
标签:des style blog io color os ar for sp
原文地址:http://www.cnblogs.com/birdhack/p/4055511.html