Subsets
Given a set of distinct 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 =
If S =
[1,2,3]
, a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
public class Solution { public ArrayList<ArrayList<Integer>> subsets(int[] S) { if(S==null) return null; Arrays.sort(S); ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); for(int i=0;i<S.length;i++){ ArrayList<ArrayList<Integer>> cur = new ArrayList<ArrayList<Integer>>(); for(ArrayList<Integer> temp:res){ cur.add(new ArrayList<Integer>(temp)); } for(ArrayList<Integer> temp:cur){ temp.add(S[i]); } ArrayList<Integer> temp1 = new ArrayList<Integer>(); temp1.add(S[i]); cur.add(temp1); res.addAll(cur); } res.add(new ArrayList<Integer>()); return res; } }
No comments:
Post a Comment