Backtracking with a start index
Time O(n · 2ⁿ) Space O(n) recursion besides the outputRecord the current path at every call, then for i from start add nums[i], recurse with i + 1, and remove it.
import java.util.*;
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> out = new ArrayList<>();
dfs(nums, 0, new ArrayList<>(), out);
return out;
}
private void dfs(int[] nums, int start, List<Integer> path, List<List<Integer>> out) {
out.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
path.add(nums[i]);
dfs(nums, i + 1, path, out);
path.remove(path.size() - 1);
}
}
}Verdict: The standard answer.