LeetCode Subsets solution details, leetcodesubsets
Returns all subsets of a given set containing different integers.
Note that the element arrangement in the subset must be non-descending, and the solution set must not contain duplicate subsets.
If S = [1, 2, 3], there is the following solution:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
// Class Solution {public List
> Subsets (int [] nums) {List
> Results = new ArrayList <> (); if (nums = null) {return results;} if (nums. length = 0) {results. add (new ArrayList
(); Return results;} Arrays. sort (nums); helper (new ArrayList
(), Nums, 0, results); return results;} private void helper (ArrayList
Subset, int [] nums, int startIndex, List
> Results) {results. add (new ArrayList
(Subset); for (int I = startIndex; I <nums. length; I ++) {subset. add (nums [I]); helper (subset, nums, I + 1, results); subset. remove (subset. size ()-1 );}}}
// Public class Solution {public List
> Subsets (int [] nums) {if (nums = null) {return null;} Arrays. sort (nums); List
> Result = new ArrayList <> (); Queue
> Que = new ArrayList <> (); que. offer (new ArrayList
(); While (! Que. isEmpty () {List
Subset = que. poll (); result. add (subset); for (int I = 0; I <nums. length; I ++) {if (subset. size () = 0 | subset. get (subset. size ()-1) <nums [I]) {List
Temp = new ArrayList <> (subset); temp. add (nums [I]); que. offer (temp) ;}} return result ;}}