Simple Java Solution


  • 0
    A
    public class Solution
    {
        public List<List<Integer>> subsets(int[] nums) 
        {
            int startIndex = 0;
            int[] count = new int[nums.length];
            Arrays.fill(count,1);
            List<Integer> inner = new ArrayList<>();
            List<List<Integer>> result = new ArrayList<>();
            result.add(new ArrayList<>(inner));
            return subsets(nums,inner,count,result,startIndex);
            
        }
        public static List<List<Integer>> subsets(int[] nums, List<Integer> inner, int[] count, List<List<Integer>> result, int startIndex)
        {
            for(int i = startIndex ; i < nums.length; i++)
            {
                    count[i] = count[i] - 1;
                    inner.add(nums[i]);
                    result.add(new ArrayList<>(inner));
                    subsets(nums,inner,count,result,i+1);
                    inner.remove(inner.size()-1);
                    count[i] = count[i] + 1;
            }
            
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.