Leetcode78/90/491之回溯中的子集问题
回溯之子集问题
- 子集问题和组合问题特别像
Leetcode78-子集
- 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)
- 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集
- 输入:nums = [1,2,3]
- 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
dfs(nums, 0);
return res;
}
private void dfs(int[] nums, int begin) {
res.add(new ArrayList<>(path));
for (int i=begin; i<nums.length; i++) {
path.add(nums[i]);
dfs(nums, i+1);
path.remove(path.size()-1);
}
}
Leetcode90-子集二
- 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)
- 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列
- 输入:nums = [1,2,2]
- 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
- 总结:一般有重复元素 如果简单的话都是排序然后判断nums[i]==nums[i-1];复杂的话用hashset(见下一题)
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(nums, 0);
return res;
}
private void dfs(int[] nums, int begin) {
res.add(new ArrayList<>(path));
for (int i=begin; i<nums.length; i++) {
if(i>0 && i!=begin && nums[i]==nums[i-1]){
continue;
}
path.add(nums[i]);
dfs(nums, i+1);
path.remove(path.size()-1);
}
}
Leetcode491-递增子序列
- 给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案
- 数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况
- 输入:nums = [4,6,7,7]
- 输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
public class L491 {
LinkedList<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> integers=new LinkedList<>();
Set<Integer> set = new HashSet<Integer>();
public List<List<Integer>> findSubsequences(int[] nums) {
find(nums,0);
return res;
}
// public void find(int[] nums,int startIndex){
// if(integers.size()>1){
// if(!res.contains(integers)){//去重
// res.add(new ArrayList<>(integers));
// }
// }
// for(int i=startIndex;i<nums.length;i++){
// if(!integers.isEmpty() && nums[i]<integers.getLast()){
// continue;
// }
// integers.add(nums[i]);
// find(nums,i+1);
// integers.removeLast();
// }
// }
public void find(int[] nums,int startIndex){
if(integers.size()>1){
res.add(new ArrayList<>(integers));
}
Set set=new HashSet<Integer>();//注意在这里new 这样每次深度递归时都是新的set
for(int i=startIndex;i<nums.length;i++){
if(!integers.isEmpty() && nums[i]<integers.getLast() || set.contains(nums[i])){
continue;
}
set.add(nums[i]);
integers.add(nums[i]);
find(nums,i+1);
integers.removeLast();
}
}
public static void main(String[] args) {
int[] test=new int[]{4,6,7,7,7};
L491 l491 = new L491();
List<List<Integer>> subsequences = l491.findSubsequences(test);
for (List<Integer> subsequence : subsequences) {
for (Integer integer : subsequence) {
System.out.print(integer);
}
System.out.println();
}
}
}
Leetcode78/90/491之回溯中的子集问题的更多相关文章
- 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理
[源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...
- 第90节:Java中的Linux基础
第90节:Java中的Linux基础 linux是装载虚拟机上面的: JDK依赖包: yum install glibc.i686 MYSQL依赖包: yum -y install libaio.so ...
- 回溯法、子集树、排列树、满m叉树
显示图: 明确给出了图中的各顶点及边 隐式图: 仅给出初始节点.目标节点及产生子节点的条件(一般有问题提议隐含给出)的情况下,构造一个图. 回溯法: 从初始状态出发,在隐式图中以深度优先的方式搜索问题 ...
- javascript父级鼠标移入移出事件中的子集影响父级的处理方法
一.我们先分析其产生的原因: 1.当鼠标从父级移入子集时触发了父级的两个事件:a.父级的mouseout事件(父级离开到子集):b.由于事件冒泡影响,又触发了父级的mouseover事件(父级移入父级 ...
- Python: 从字典中提取子集--字典推导
问题: 构造一个字典,它是另外一个字典的子集 answer: 最简单的方式是使用字典推导 eg1: 1. >>>prices = {'ACME': 45.23, 'AAPL': 61 ...
- 【leetcode题目整理】数组中找子集
368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...
- 【python cookbook】【数据结构与算法】17.从字典中提取子集
问题:想创建一个字典,其本身是另一个字典的子集 解决方案:利用字典推导式(dictionary comprehension)可轻松解决 # example of extracting a subset ...
- 第90天:HTML5中文件API和拖放操作
一.文件API File API:提供客户端本地操作文件的可能 multiple是让文件域可以多选 <!DOCTYPE html> <html lang="en" ...
- java中list或数组中随机子集工具类
package com.example.demo.test; import java.util.ArrayList;import java.util.Arrays;import java.util.L ...
随机推荐
- LGP5591题解
题意很明确,不说了. 前置芝士:单位根反演 也就是: \[[n|a]=\frac 1 n \sum_{i=0}^{n-1}w_n^{ai} \] 看到题目给的柿子: \[\sum_{i=0}^n\bi ...
- Net中事件的高级用法之三
1.事件的高级应用 使用事件可以解除代码耦合 2.事件高级应用实例 using System; using System.Collections.Generic; using System.Linq; ...
- instanceOf与父子类型转换
instanceOf 只要有父子类关系,sout(A instanceOf B)结果就位ture,反之false 先前定义 class Person ; method calss Father ; m ...
- Spring MVC 04-- 接收前端参数json格式的方式
/** * 第一种:以RequestParam接收 * 前端传来的是json数据不多时:{"id":1},可以直接用@RequestParam来获取值 * * @param id ...
- Ubuntu18.04..5 配置国内镜像源:解决E: Failed to fetch
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 问题描述 使用 sudo apt get-install 出现 E: Failed to fetch问题. 更换镜像源 错误原因:绝大多数情况下, ...
- JS 邮箱的验证(正则)
/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/
- 各种环境下反弹shell
0x00 NC命令详解 在介绍如何反弹shell之前,先了解相关知识要点. nc全称为netcat,所做的就是在两台电脑之间建立链接,并返回两个数据流 可运行在TCP或者UDP模式,添加参数 -u 则 ...
- 如何在vscode中编写.net core 项目(vscode)
1.下载拓展 .NET Core Extension Pack (作者:保哥) 这个里面将需要的插件都打包了小白一键下载就好了 2.下载扩展 vscode-solution-explorer ...
- Java基础 (下)
泛型 Java 泛型了解么?什么是类型擦除?介绍一下常用的通配符? Java 泛型(generics) 是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时 ...
- python+pytest接口自动化(12)-自动化用例编写思路 (使用pytest编写一个测试脚本)
经过之前的学习铺垫,我们尝试着利用pytest框架编写一条接口自动化测试用例,来厘清接口自动化用例编写的思路. 我们在百度搜索天气查询,会出现如下图所示结果: 接下来,我们以该天气查询接口为例,编写接 ...