Subsets

Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Iterator

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList();
        res.add(new ArrayList());
        if(nums.length == 0) return res;
        Arrays.sort(nums);
        for(int i = 0 ; i < nums.length; i++){
            int n = nums[i], size = res.size();
            for(int j = 0 ; j < size; j++){
                List <Integer> arr = new ArrayList();
                arr.addAll(res.get(j));
                arr.add(n);
                res.add(arr);
            }
        }
        return res;
    }

results matching ""

    No results matching ""