java算法训练------回溯算法------子集(I 、II)

1.子集 I
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums 中的所有元素 互不相同

思路:想了很久想法,一开始觉得应该从长度为1的子集开始收集,但是发现,我没有办法把递归的出口想出来。那就只能从最长的子集下手,这样就会有递归的出口了,那就是为index == nums.length,通过递归的调用,从最长子集中,不断地删除元素,再通过调用添加元素即可。
代码:

import java.util.*;

/**
 * @author 龙小虬
 * @date 2021/4/1 1:50
 */

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] nums = new int[3];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        System.out.println(subsetsWithDup(nums));
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        HashSet<List<Integer>> hashSet = new HashSet<>();
        dfs(nums, 0,temp,res,hashSet);
        return res;
    }


    public static void dfs(int[] nums, int index,List<Integer> temp,List<List<Integer>> res,HashSet<List<Integer>> hashSet) {
        if (index == nums.length) {
            res.add(new ArrayList<>(temp));
            return;
        }
        temp.add(nums[index]);
        dfs(nums, index + 1,temp,res,hashSet);
        temp.remove(temp.size() - 1);
        dfs(nums, index + 1,temp,res,hashSet);
    }

}

题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subsets



2.子集 II

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

1 <= nums.length <= 10
-10 <= nums[i] <= 10

思路:这里的升级版本和之前的老版本区别在于有同一元素,因为有同一元素的存在,所以我们必须要进行去重。因为最近在研究HashMap,所以直接就想到了使用HashSet来存储数据,来达到去重的目的。
代码:

import java.util.*;

/**
 * @author 龙小虬
 * @date 2021/4/1 1:50
 */

public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] nums = new int[3];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        System.out.println(subsetsWithDup(nums));
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        HashSet<List<Integer>> hashSet = new HashSet<>();
        dfs(nums, 0,temp,res,hashSet);
        return res;
    }


    public static void dfs(int[] nums, int index,List<Integer> temp,List<List<Integer>> res,HashSet<List<Integer>> hashSet) {
        if (index == nums.length) {
            if(hashSet.add(new ArrayList<>(temp))){
                res.add(new ArrayList<>(temp));
            }
            return;
        }
        temp.add(nums[index]);
        dfs(nums, index + 1,temp,res,hashSet);
        temp.remove(temp.size() - 1);
        dfs(nums, index + 1,temp,res,hashSet);
    }

}

代码如上,但是我最后还是没有通过
案例:

输入:
[4,4,4,1,4]
输出:
[[4,4,4,1,4],[4,4,4,1],[4,4,4,4],[4,4,4],[4,4,1,4],[4,4,1],[4,4],[4,1,4],[4,1],[4],[1,4],[1],[]]
预期:
[[],[1],[1,4],[1,4,4],[1,4,4,4],[1,4,4,4,4],[4],[4,4],[4,4,4],[4,4,4,4]]

很明显,这段代码的输出还是有同一子集的(因为子集中的元素是无序性的),那要怎么去解决这个问题?其实很简单,排序之后不久可以了?因为我们没有完全去重的原因是他的无序性,我们把它变成子集元素升序或者降序不就好了。
代码:

import java.util.*;

/**
 * @author 龙小虬
 * @date 2021/4/1 1:50
 */

public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] nums = new int[3];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        Arrays.sort(nums);
        System.out.println(subsetsWithDup(nums));
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        HashSet<List<Integer>> hashSet = new HashSet<>();
        dfs(nums, 0,temp,res,hashSet);
        return res;
    }


    public static void dfs(int[] nums, int index,List<Integer> temp,List<List<Integer>> res,HashSet<List<Integer>> hashSet) {
        if (index == nums.length) {
            if(hashSet.add(new ArrayList<>(temp))){
                res.add(new ArrayList<>(temp));
            }
            return;
        }
        temp.add(nums[index]);
        dfs(nums, index + 1,temp,res,hashSet);
        temp.remove(temp.size() - 1);
        dfs(nums, index + 1,temp,res,hashSet);
    }

}

题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subsets-ii

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙小虬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值