90 subsets II

https://leetcode.com/problems/subsets-ii/

่ฟ™ไธชๅŽป้‡็š„ๆ€่ทฏๆˆ‘ๆ„Ÿ่ง‰็œŸ็š„ๅพˆๅฅฝ็”จ๏ผ

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        dfs(nums, 0, new ArrayList<>());
        return ans;
    }
    private void dfs(int[] nums, int index, List<Integer> path) {
        if (index == nums.length) {
            ans.add(new ArrayList<>(path));
            return;
        }
        int end = index;
        while (end < nums.length && nums[end] == nums[index]) end++;
        dfs(nums, end, path);
        int presize = path.size();
        for (int i = index; i < end; i++) {
            path.add(nums[i]);
            dfs(nums, end, path);
        }
        path.subList(presize, path.size()).clear();
    }
}

Last updated