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