Java straightforward recursive solution


  • 0
    J
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            sets(nums, 0, new ArrayList<>(), result);
            return result;
        }
        
        private void sets(int[] nums, int start, ArrayList<Integer> path, List<List<Integer>> result) {
            if (start >= nums.length) {
                List<Integer> res = new ArrayList<>();
                res.addAll(path);
                result.add(res);
                return;
            }
            path.add(nums[start]);
            sets(nums, start + 1, path, result);
            path.remove(path.size() - 1);
            sets(nums, start + 1, path, result);
        }
    

Log in to reply
 

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