Standard DFS Java solution


  • 0
    Z
    public class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
            dfs(nums,0,new ArrayList<Integer>(),result);
            return result;
        }
        
        public void dfs(int[] nums, int index,List<Integer> path, List<List<Integer>> result){
            if(index<=nums.length){
                result.add(path);
            }
            for(int i=index;i<nums.length;i++){
                List<Integer> nPath= new ArrayList<>(path);
                nPath.add(nums[i]);
                dfs(nums,i+1,nPath,result);
            }
        }
    }

Log in to reply
 

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