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;
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • RMFOREVER123

    The code is wrong

  • DivyaJyoti Rajdev

    actually I wouldn’t go the master theorem way, because for that the ‘aT(n/b)’ term needs to be defined, here a_n = 2a_(n-1) + f(n).. its hard to visualize it in terms of T(n/b), i would simply explain it as for every element i: work done = 2*2^(i-1) [using a_n=2*a_(n-1)] so for a^n=2^n, base case a_0=1 (considering 0 elements, empty subset)

  • Sam Goodwin

    Much simpler in Scala

    def allSubsets(S: List[Int]) = {
    S.distinct.sorted.foldLeft(List(List.empty[Int])) { (subsets, num) ⇒
    subsets ++ subsets.map(_ :+ num)
    }
    }

  • Mahendhar

    I know the time complexity is 2 power n, how do i get there with a mathematical formula? anyone please?, i was asked this in a startup interview today! I tried with masters theorem but couldn’t get there.

  • will

    Hi,

    I’ve got a shorter code for this problem.

    public ArrayList<ArrayList> subsets(int[] S) {

    ArrayList<ArrayList> ans = new ArrayList<ArrayList>();

    ans.add(new ArrayList());

    Arrays.sort(S);

    for (int i = 0; i < S.length; i ++) {

    int curSize = ans.size();

    for (int j = 0; j < curSize; j ++) {

    ArrayList cur = new ArrayList(ans.get(j));

    cur.add(S[i]);

    ans.add(cur);

    }

    }

    return ans;

    }