Simple DFS Java solution beating 98%


  • 0
    H

    public class Solution {

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        ArrayList<Integer> list = new ArrayList<Integer>();
        helper(nums, 0, list, res);
        return res;
    }
    private void helper(int[] nums, int idx, ArrayList<Integer> list, List<List<Integer>> res){
        res.add(list);
        for(int i=idx;i<nums.length;i++){
            ArrayList<Integer> _list = (ArrayList)list.clone();
            _list.add(nums[i]);
            helper(nums, i+1, _list, res);
        }
    }
    

    }


Log in to reply
 

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