LeetCode – Subsets (Java)
Given a set of distinct integers, S, return all possible subsets.
Note: 1) Elements in a subset must be in non-descending order. 2) The solution set must not contain duplicate subsets.
For example, given S = [1,2,3], the method returns: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Thoughts
Given a set S of n distinct integers, there is a relation between Sn and Sn-1. The subset of Sn-1 is the union of {subset of Sn-1} and {each element in Sn-1 + one more element}. Therefore, a Java solution can be quickly formalized.
Java Solution
public ArrayList<ArrayList<Integer>> subsets(int[] S) { if (S == null) return null; Arrays.sort(S); ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < S.length; i++) { ArrayList<ArrayList<Integer>> temp = new ArrayList<ArrayList<Integer>>(); //get sets that are already in result for (ArrayList<Integer> a : result) { temp.add(new ArrayList<Integer>(a)); } //add S[i] to existing sets for (ArrayList<Integer> a : temp) { a.add(S[i]); } //add S[i] only as a set ArrayList<Integer> single = new ArrayList<Integer>(); single.add(S[i]); temp.add(single); result.addAll(temp); } //add empty set result.add(new ArrayList<Integer>()); return result; } |
<pre><code> String foo = "bar"; </code></pre>
-
SVS
-
Rishabh Yadav
-
Rishabh Yadav
-
Alex
-
RMFOREVER123
-
DivyaJyoti Rajdev
-
Sam Goodwin
-
Mahendhar
-
will