78题目
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
暴力扩展法处理
import java.util.ArrayList;
import java.util.List;
//暴力扩展法
/*
思想: 先加入空集,在该集合的基础上加入下一个元素,数组[1,2,3]
1、先加入[] 结果集={[]},
2、再加入1,结果集={[],[1]} //空集的基础上加入1
3、再加入2,结果集={[],[1],[2],[1,2]} //{[],[1]}的基础上加入2
4、再加入3,结果集={[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]} //同理
*/
public class subsets_78_0 {
public List<List<Integer>> subsets(int[] nums){
//创建一个可以存储List的空集(result)
List<List<Integer>> result = new ArrayList<List<Integer>>();
//创建一个List的空集传入result。因为空集是所有集合的子集,因此无论数组中有没有值都会包括空集
result.add(new ArrayList<>());
//迭代器遍历每一个元素并加入已存在的集合中
for (int num : nums) {
int resLen = result.size();
for (int i = 0; i < resLen; i++){
List<Integer> cur = new ArrayList<>(result.get(i));
cur.add(num);
result.add(cur);
}
}
return result;
}
public static void main(String[] args) {
int[] arr = {
1, 2, 3};
subsets_78_0 s0 = new subsets_78_0();
System.out.println(s0.subsets(arr));
}
}
backtracking算法处理
import java.util.ArrayList;
import java.util.List;
//backtracking
/*
* 算法思想:按照数组的长度依次处理问题。
* 1、当长度为0时直接返回一个空集合加入到结果集中,
* 2、当长度为1时依次遍历加入当结果集当中,
* 3、需要注意的是在backtracking中一次for循环结束后要保证每次cur子集为空子集(因为在边界处理时[if中]
* 当前cur已经加入到了res结果集当中),避免
* 4、当长度为2时依次添加两个元素加入res结果集当中,同理保证bt中每次for循环结束一次后的cur为空
* 5、长度为n时同理
* */
public class subsets_78_1 {
public List<List<Integer>> subSets(int[] nums){
List<List<Integer>> res = new ArrayList<>();
//按照长度依次处理
for (int i = 0; i <= nums.length; i++){
backtracking(nums, i, 0, res, new ArrayList<>());
}
return res;
}
//传入数组,长度,当前索引值,结果集,结果集中的子集
public void backtracking(int[] nums, int len, int startIndex, List<List<Integer>> res, List<Integer> cur){
if (cur.size() == len){
res.add(new ArrayList<>(cur));
return;
}
for (int i = startIndex; i < nums.length; i++){
cur.add(nums[i]);
backtracking(nums, len, i+1, res, cur);
cur.remove(cur.size() - 1); // 删除,eg:处理长度为1时的情况,如果不删除,只能够处理第一个元素
}
}
public static void main(String[] args) {
int[] arr = {
1, 2, 3};
subsets_78_1 s0 = new subsets_78_1();
System.out.println(s0.subSets(arr));
}
}
DFS算法处理
import java.util.ArrayList;
import java.util.List;
/*
* DFS思想:空集作为解空间树的第一层;第二层每个节点加入一个元素(数组中的元素依次加入),每个元素作为一个节点;第三层的节点在第二层节点的基础上再加入一个元素;
* 核心是每个节点生成时只能依次加入上层节点中最后一个元素的后几个元素。
* */
public class subsets_78_2 {
public List<List<Integer>> subsets(int[] nums){
List<List<Integer>> res = new ArrayList<>();
dfs(nums, 0, res, new ArrayList<>());
return res;
}
public void dfs(int[] nums, int startIndex, List<List<Integer>> res, List<Integer> cur){
res.add(new ArrayList<>(cur));
for (int i = startIndex; i < nums.length; i++){
cur.add(nums[i]);
dfs(nums, i+1, res, cur);
cur.remove(cur.size()-1);
}
}
public static void main(String[] args) {
int[] arr = {
1, 2, 3};
subsets_78_2 s0 = new subsets_78_2();
System.out.println(s0.subsets(arr));
}
}
90题目
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: [1,2,2]
输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
暴力扩展法: 在78题基础上利用set集合处理一下重复和List中相同元素
public List<List<Integer>> subs(int[] nums){
List<List<Integer>> l = new ArrayList<>();
l.add(new ArrayList<>());
Arrays.sort(nums); //排序的目的是为了避免此出现[1,2] 和[2,1]的情况同时出现
for (int i = 0; i < nums.length; i++){
int resLen = l.size();
for (int j = 0; j < resLen; j++){
List<Integer> cur = new ArrayList<>(l.get(j));
cur.add(nums[i]);
l.add(cur);
}
}
//去重
HashSet<List<Integer>> set = new HashSet<>(l);
return new ArrayList<>(set);
}
backtracking算法处理: 在78题基础上增加了排序和去重
//backtracking
public List<List<Integer>> subSets(int[] nums){
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
//按照长度依次处理
for (int i = 0; i <= nums.length; i++){
backtracking(nums, i, 0, res, new ArrayList<>());
}
return res;
}
public void backtracking(int[] nums, int len, int startIndex, List<List<Integer>> res, List<Integer> cur){
if (cur.size() == len){
res.add(new ArrayList<>(cur));
return;
}
for (int i = startIndex; i < nums.length; i++){
if (i > startIndex && nums[i] == nums[i-1]){
continue;
}
cur.add(nums[i]);
backtracking(nums, len, i+1, res, cur);
cur.remove(cur.size() - 1);
}
}
DFS算法处理: 同理在78题基础上添加了排序和去重
public List<List<Integer>> subsets(int[] nums){
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
dfs(nums, 0, res, new ArrayList<>());
return res;
}
public void dfs(int[] nums, int startIndex, List<List<Integer>> res, List<Integer> cur){
res.add(new ArrayList<>(cur));
for (int i = startIndex; i < nums.length; i++){
if (i > startIndex && nums[i] == nums[ i-1 ]){
continue;
}
cur.add(nums[i]);
dfs(nums, i+1, res, cur);
cur.remove(cur.size()-1);
}
}