代码随想录算法刷题训练营day29:LeetCode(491)递增子序列、LeetCode(46)全排列、LeetCode(47)全排列 II

代码随想录算法刷题训练营day29:LeetCode(491)递增子序列、LeetCode(46)全排列、LeetCode(47)全排列 II

LeetCode(491)递增子序列
题目
在这里插入图片描述
代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
    //先取出来再定义一个函数进行判断
    public Set<List<Integer>> tempResult=new HashSet<>();
    public List<Integer> path=new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        int startIndex=0;
        backtracking(nums,startIndex);
        List<List<Integer>> result=new ArrayList<>(tempResult);
        return result;
    }
    public void backtracking(int[] nums,int startIndex){
        if(isSubsequences(path)){
            List<Integer> tempPath=Arrays.asList(new Integer[path.size()]);
            Collections.copy(tempPath, path);
            tempResult.add(tempPath);//子集问题不能加return
        }
        for(int i=startIndex;i<nums.length;i++){
            path.add(nums[i]);
            backtracking(nums, i+1);
            path.remove(path.size()-1);
        }
    }
    public boolean isSubsequences(List<Integer> path){
        boolean flag=true;
        if(path.size()<2)
        {
            flag=false;
        }
        List<Integer> testPath=Arrays.asList(new Integer[path.size()]);
        Collections.copy(testPath, path);
        Collections.sort(testPath);
        for(int i=0;i<path.size();i++){
            if(testPath.get(i)!=path.get(i)){
                flag=false;
            }
        }
        return flag;
    }
}

LeetCode(46)全排列
题目
在这里插入图片描述

代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Solution {
    public List<List<Integer>> result=new ArrayList<>();
    public List<Integer> path=new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        int[] used=new int[nums.length];//记录元素是否被使用
        int startIndex=0;
        backtracking(nums,used);        
        return result;
    }
    public void backtracking(int[] nums,int[] used){
        if(path.size()==nums.length){
            List<Integer> tempPath=Arrays.asList(new Integer[path.size()]);
            Collections.copy(tempPath, path);
            result.add(tempPath);
            return;
        }
         for(int i=0;i<nums.length;i++){
            if(used[i]==1){
                continue;//1-1这个分支直接没有了,直此时就能到1-2了--重点
            }
            path.add(nums[i]);
            used[i]=1;
            backtracking(nums, used);
            path.remove(path.size()-1);
            used[i]=0;//弹出来状态也得改变
         }
        
    }
}

LeetCode(47)全排列 II
题目
在这里插入图片描述

代码

java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
    public Set<List<Integer>> tempResult=new HashSet<>();//用set集合做
    public List<Integer> path=new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        int[] used=new int[nums.length];
        backtracking(nums,used);
        List<List<Integer>> result=new ArrayList<>(tempResult);
        return result;
    }
    public void backtracking(int[] nums,int[] used){
        if(path.size()==nums.length){
            List<Integer> tempPath=Arrays.asList(new Integer[path.size()]);
            Collections.copy(tempPath, path);
            tempResult.add(tempPath);
            return;
        }
        for(int i=0;i<nums.length;i++){
            if(used[i]==1){
                continue;
            }
            path.add(nums[i]);
            used[i]=1;
            backtracking(nums, used);
            path.remove(path.size()-1);
            used[i]=0;
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/420596.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Web】青少年CTF擂台挑战赛 2024 #Round 1 wp

好家伙&#xff0c;比赛结束了还有一道0解web题是吧( 随缘写点wp(简单过头&#xff0c;看个乐就好) 目录 EasyMD5 PHP的后门 PHP的XXE Easy_SQLi 雏形系统 EasyMD5 进来是个文件上传界面 说是只能上传pdf&#xff0c;那就改Content-Type为application/pdf&#xff0c;改…

使用R语言进行Logistic回归分析(2)

一、数据集描述&#xff0c;问题要求 下表是40位肺癌病人的生存资料&#xff0c;X1表示生活行为能力平分&#xff08;1到100&#xff09;&#xff0c;X2为病人的年龄&#xff08;年&#xff09;&#xff0c;X3由诊断到进入研究的时间&#xff08;月&#xff09;&#xff0c;X4…

【Android开发】01-第一个Android APP

一、改MainActivity class MainActivity : AppCompatActivity() {/*因Android的app有生命周期&#xff0c;故入口是OnCreate而不是main函数*/override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main…

Unity编写Shader内置各种矩阵和方法介绍

嗨&#xff0c;各位小伙伴们&#xff0c;我是你们的好朋友咕噜铁蛋&#xff01;今天&#xff0c;我们要来聊一聊关于Unity中编写Shader时内置的各种矩阵和方法。作为Unity开发者&#xff0c;掌握Shader编写是非常重要的一项技能&#xff0c;而了解内置的矩阵和方法将帮助我们更…

LeetCode383. 赎金信(C++)

LeetCode383. 赎金信 题目链接代码 题目链接 https://leetcode.cn/problems/ransom-note/description/ 代码 class Solution { public:bool canConstruct(string ransomNote, string magazine) {int record[26] {0};if(ransomNote.size() > magazine.size()) return fa…

利用networkx做固定坐标的样例图

图技术 利用neo4j、networkx、dgl、python做图分析挖掘 【1】最短路径算法dijkstra 【2】基于networkx的隐性集团关系识别模型 【3】基于Neo4j的担保社群型态分析挖掘 【4】基于python求有向无环图中target到其他节点全路径 【5】有向图中任意两点的路径 【6】图基础入门 【7】…

c++之运算符,程序流程结构

运算符 作用&#xff1a;用于执行代码的运算 1算术运算符 作用&#xff1a;用于处理四则运算 下面我们用代码展示&#xff1a; #include<iostream> using namespace std; int main() {//加减乘除int a1 10;int b1 3;cout <<" a1 b1 "<< a1…

Python小白必学的面向对象

我们已经知道在Python中“一切皆对象”&#xff0c;每个对象都有特定的类型&#xff0c;现在让我们来尝试创建自己的类型——这需要使用class关键字来定义新的“类”&#xff08;Class&#xff09;&#xff0c;类是用来生成对象的“模板”&#xff0c;对象则是其所属类的“实例…

Codeforces Round 930 (Div. 2)(A~B)补题

Codeforces Round 930 Div. 2 A. Shuffle Party1、模拟过程2、代码 B. Binary Path1、过程模拟2、代码 A. Shuffle Party A. Shuffle Party A. Shuffle Partytime limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard inputoutput: standard outpu…

2024年3月2日 十二生肖 今日运势

小运播报&#xff1a;2024年3月2日&#xff0c;星期六&#xff0c;农历正月廿二 &#xff08;甲辰年丙寅月乙丑日&#xff09;&#xff0c;法定节假日。 红榜生肖&#xff1a;鸡、蛇、鼠 需要注意&#xff1a;狗、马、羊 喜神方位&#xff1a;西北方 财神方位&#xff1a;东…

【刷题】位运算

消失的两个数字 消失的两个数字 “单身狗”进阶版思路 class Solution { public:vector<int> missingTwo(vector<int>& nums) {int ret 0;int n nums.size();for(int i 0; i < n; i){ret ^ (nums[i] ^ i);}ret ^ (n ^ (n 1) ^ (n 2));// 按位异或的…

基于SpringBoot的综合小区管理系统的设计与实现

文章目录 项目介绍主要功能截图&#xff1a;部分代码展示设计总结项目获取方式 &#x1f345; 作者主页&#xff1a;超级无敌暴龙战士塔塔开 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、 简历模板、学习资料、面试题库【关注我&#xff0c;都给你】 &…

Linux 安装k8s

官网 常见的三种安装k8s方式 1.kubeadm 2.kops&#xff1a;自动化集群制备工具 3.kubespray&#xff1a; 提供了 Ansible Playbook 下面以kubeadm安装k8s kubeadm的安装是通过使用动态链接的二进制文件完成的&#xff0c;目标系统需要提供 glibc ##使用 ss 或者 netstat 检测端…

.halo勒索病毒的最新威胁:如何恢复您的数据?

尊敬的读者&#xff1a; 随着科技的发展&#xff0c;网络安全已经成为我们日常生活中不可忽视的重要议题。其中&#xff0c;勒索病毒是当前网络安全威胁中的一大挑战&#xff0c;而“.halo”勒索病毒更是近期备受关注的恶意软件之一。本文将介绍关于“.halo”勒索病毒的背景知…

循环简介和基本运算符

根据C Primer Plus第五章进行学习 文章目录 循环简介基本运算符 1.赋值运算符&#xff1a;2.加法运算符&#xff1a;3.减法运算符&#xff1a;-2.乘法运算符&#xff1a;*总结 1.循环简介 如下代码可以体现不使用循环的局限性&#xff1a; #include<stdio.h> #define AD…

【C语言】linux内核xmit_one函数

一、中文注释 static int xmit_one(struct sk_buff *skb, struct net_device *dev,struct netdev_queue *txq, bool more) {unsigned int len;int rc;// 如果全局ptype列表或者设备特定的ptype列表不为空&#xff0c;则执行网络接口层网络层的NIT&#xff08;Network Tap&…

【C++】vector的使用及其模拟实现

这里写目录标题 一、vector的介绍及使用1. vector的介绍2. 构造函数3. 遍历方式4. 容量操作及空间增长问题5. 增删查改6. vector二维数组 二、vector的模拟实现1. 构造函数2. 迭代器和基本接口3. reserve和resize4. push_back和pop_back5. insert和erase5. 迭代器失效问题5. 浅…

docker 基础(二)

常见命令 Docker最常见的命令就是操作镜像、容器的命令&#xff0c;详见官方文档&#xff1a;https://docs.docker.com/ 数据卷 命令说明文档地址docker volume create创建数据卷docker volume createdocker volume ls创建数据卷docker volume lsdocker volume rm查看所有数…

NGINX 高频面试题及实践总结

NGINX 是一个高性能的开源 Web 服务器和反向代理服务器&#xff0c;被广泛应用于互联网架构中。在面试中&#xff0c;对 NGINX 的相关知识可能会成为考察的重点。下面我们整理了一些常见的 NGINX 面试题及答案&#xff0c;希望对大家在面试前的准备有所帮助。 ## 1. 什么是 NG…

如何系统性的学习推荐系统?

推荐一本适合推荐系统、计算广告、个性化搜索领域的从业人员阅读的书&#xff1a;《互联网大厂推荐算法实战》。快手公司算法专家10余年的实战经验总结。涵盖一线互联网公司当前采用的主流推荐算法&#xff0c;凸显可用性、实用性提供从算法基本原理&#xff0c;到技术框架再到…