LEETCODE-DAY29


title: LEETCODE-DAY29
date: 2024-03-20 15:22:38
tags:

今日内容:491.递增子序列、46.全排列、47.全排列 II

T1

class Solution:def backtracking(self,nums,index,path,res):if index==len(nums):res.append(path.copy())returnfor i in range(index,len(nums)):if i> index and nums[i]<nums[i-1]:continuepath.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

输入
nums =
[4,4,3,2,1]
输出
[[4,4,3,2,1],[4,3,2,1]]
预期结果
[[4,4]]

class Solution:def backtracking(self,nums,index,path,res):if index==len(nums):res.append(path.copy())returnfor i in range(index,len(nums)):if i> index and nums[i]>=nums[i-1]:path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()else:continuedef findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

输入
nums =
[4,4,3,2,1]
输出
[]
预期结果
[[4,4]]

nums =
[4,6,7,7]
输出
[[6,7],[7]]
预期结果
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

逻辑错了应该跟path最后一位比较

class Solution:def backtracking(self,nums,index,path,res):if index==len(nums):res.append(path.copy())returnfor i in range(index,len(nums)):path.append(nums[i])if len(path)>=2 and path[-1]<path[-2]:    returnself.backtracking(nums,i+1,path,res)path.pop()def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

输入
nums =
[4,6,7,7]
输出
[[4,6,7,7],[4,6,7],[4,7,7],[4,7],[6,7,7],[6,7],[7,7],[7]]
预期结果
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

输入
nums =
[4,4,3,2,1]
输出
[]
预期结果
[[4,4]]

应该是收集结果的判断有问题
[4,6]满足递增条件但是还没到队列尾部所以没有放入res中

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2 and path[-1]>=path[-2]:res.append(path.copy())returnfor i in range(index,len(nums)):path.append(nums[i])if len(path)>=2 and path[-1]<path[-2]:    returnself.backtracking(nums,i+1,path,res)path.pop()def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

输入
nums =
[4,6,7,7]
输出
[[4,6],[4,7],[4,7],[6,7],[6,7],[7,7]]
预期结果
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
貌似也不对

因为是取树上的节点所以不要return

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2 and path[-1]>=path[-2]:#此处直接写if len(path)>=2: 结果一样res.append(path.copy())for i in range(index,len(nums)):path.append(nums[i])if len(path)>=2 and path[-1]<path[-2]:    returnself.backtracking(nums,i+1,path,res)path.pop()def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

nums =
[4,4,3,2,1]
输出
[[4,4],[4,4,4]]
预期结果
[[4,4]]

输入
nums =
[4,6,7,7]
输出
[[4,6],[4,6,7],[4,6,7,7],[4,6,7],[4,7],[4,7,7],[4,7],[6,7],[6,7,7],[6,7],[7,7]]
预期结果
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
有重复

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2 :res.append(path.copy())for i in range(index,len(nums)):if not path:path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()else:if nums[i]>=path[-1]:path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()else:return'''def backtracking(self,nums,index,path,res):if len(path)>=2  :res.append(path.copy())for i in range(index,len(nums)):if path and nums[i]<path[-1]:return(continue)path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()   结果一样'''            def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

输入
nums =
[4,4,3,2,1]
输出
[[4,4]]
预期结果
[[4,4]]

输入
nums =
[4,6,7,7]
输出
[[4,6],[4,6,7],[4,6,7,7],[4,6,7],[4,7],[4,7,7],[4,7],[6,7],[6,7,7],[6,7],[7,7]]
预期结果
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2 and path not in res :res.append(path.copy())for i in range(index,len(nums)):if not path:path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()else:if nums[i]>=path[-1]:path.append(nums[i])self.backtracking(nums,i+1,path,res)path.pop()else:returndef findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

用例过了但没AC

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2  :res.append(path.copy())used=set()    for i in range(index,len(nums)):if (path and nums[i]<path[-1]) or nums[i] in used:returnpath.append(nums[i])used.add(nums[i])self.backtracking(nums,i+1,path,res)path.pop()               def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

18 / 58 个通过的测试用例

class Solution:def backtracking(self,nums,index,path,res):if len(path)>=2  :res.append(path.copy())used=set()    for i in range(index,len(nums)):if (path and nums[i]<path[-1]) or nums[i] in used:continuepath.append(nums[i])used.add(nums[i])self.backtracking(nums,i+1,path,res)path.pop()               def findSubsequences(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,0,[],res)return res

return与continue的区别
return:函数终止
continue:for 循环中 这一层的i终止进入i+1

T2

class Solution:def backtracking(self,nums,path,res):if len(path)==len(nums):res.append(path)returnused=list()for i in range(len(nums)):if nums[i] in used: continuepath.append(nums[i])self.backtracking(nums,path,res)path.pop()def permute(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,[],res)return res

输入
nums =
[0,1]
输出
[[],[],[],[]]
预期结果
[[0,1],[1,0]]

class Solution:def backtracking(self,nums,path,res,used):if len(path)==len(nums):res.append(path)returnfor i in range(len(nums)):if nums[i] in used: continuepath.append(nums[i])used.append(nums[i])self.backtracking(nums,path,res,used)path.pop()def permute(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,[],res,[])return res

输入
nums =
[1,2,3]
输出
[[]]
预期结果
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

细节忘记path.copy了

class Solution:def backtracking(self,nums,path,res,used):if len(path)==len(nums):res.append(path.copy())returnfor i in range(len(nums)):if nums[i] in used: continuepath.append(nums[i])used.append(nums[i])self.backtracking(nums,path,res,used)path.pop()def permute(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,[],res,[])return res

输入
nums =
[0,1]
输出
[[0,1]]
预期结果
[[0,1],[1,0]]

要注意used数组也要相应的做回溯

class Solution:def backtracking(self,nums,path,res,used):if len(path)==len(nums):res.append(path.copy())returnfor i in range(len(nums)):if nums[i] in used: continuepath.append(nums[i])used.append(nums[i])self.backtracking(nums,path,res,used)path.pop()used.pop()def permute(self, nums: List[int]) -> List[List[int]]:res=[]self.backtracking(nums,[],res,[])return res

AC

细节上还是要注意

T3

class Solution:def backtracking(self,nums,path,res,used):if len(path)==len(nums):if path not in res:res.append(path.copy())returnfor i in range(len(nums)):if used[i]:continuepath.append(nums[i])used[i]=Trueself.backtracking(nums,path,res,used)path.pop()used[i]=Falsedef permuteUnique(self, nums: List[int]) -> List[List[int]]:res=[]used=[False]*(len(nums))self.backtracking(nums,[],res,used)return res

这是暴力去重,还有简化解法,后补



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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot项目学习-瑞吉外卖(4)

1.任务 这一节主要的任务是解决文件的上传和下载功能 2.文件上传 概念&#xff1a;将本地的图片上传到浏览器上面 点击文件上传&#xff0c;前端就会发送如上的请求&#xff0c;服务端应该根据URL和请求方法来处理请求 CommonController类&#xff1a; RestController Slf4j …

【第二部分--Python之基础】

一、初识 开发语言&#xff1a; 高级语言&#xff1a;Python Java PHP C# Go Ruby C ... > 字节码 低级语言&#xff1a;C 汇编 > 机器码 …

6、jenkins项目构建类型1-项目类型介绍

文章目录 一、自由风格项目二、Maven项目构建三、Pipeline流水线项目构建&#xff08;☆☆☆&#xff09; Jenkins中自动构建项目的类型有很多&#xff0c;常用的有以下三种&#xff1a; 自由风格软件项目&#xff08;FreeStyle Project&#xff09;Maven项目&#xff08;Mave…

初始Redis关联和非关联

基础篇Redis 3.初始Redis 3.1.2.关联和非关联 传统数据库的表与表之间往往存在关联&#xff0c;例如外键&#xff1a; 而非关系型数据库不存在关联关系&#xff0c;要维护关系要么靠代码中的业务逻辑&#xff0c;要么靠数据之间的耦合&#xff1a; {id: 1,name: "张三…

Maya FBX导出导入

问题描述&#xff1a; Maya 导出导入 FBX&#xff0c;设置 FBX 的 导入和导出设置 解决方案&#xff1a; 获取FBX设置 def getFBXSettings():""" get current user settings for FBX export and store them """mel.eval(FBXPushSettings;)de…

指针知识大礼包,让你的编程之路更顺畅(一)

1. 内存和地址 2. 指针变量和地址 3. 指针变量类型的意义 4. const修饰指针 5. 指针运算 6. 野指针 7. assert断⾔ 8. 指针的使⽤和传址调⽤ 正文开始 1. 内存和地址 1.1 内存 在讲内存和地址之前&#xff0c;我们想有个⽣活中的案例&#xff1a; 假设有⼀栋宿舍楼&a…

自锁电路设计

自锁电路设计 Hi,uu们,是不是经常要用到自锁电路,通常不是使用555芯片就是用比较器来做自锁,今天我们来简单看下自锁电路的设计.图1采用了比较器构建了一个自锁电路,采用低电平复位&#xff0c;当需要复位的时候志需要将反向端的二极管拉低一下即可&#xff0c;免去 三极管控制…

glsl改变纹理坐标来放大图像

使用glsl中的顶点着色器来改变坐标位置 // 顶点着色器 #version 330 core layout (location 0) in vec2 Position; // 位置坐标 layout (location 1) in vec2 TexCoord; // 原始纹理坐标 out vec2 FragTexCoord; // 输出给片段着色器的纹理坐标 void main() {gl_Position …

springboot 大文件分片上传

springboot 大文件分片上传 constantentityvocontrollerutils大文件分片上传是一种将大文件分割成多个小文件片段,然后分别上传这些小文件片段的方法。这种方法的好处包括: 减少重新上传开销:如果网络传输中断,只需重传未上传的部分,而不是整个文件。 提高灵活性:分片大小…

产品经理和市场经理的区别

1. 前言 行业权威人士强调,高达九成的产品在市场上未达预期目标,被视为失败。尽管你的产品未必在此范围内,我依然认为市场上存在大量构思粗糙、尚未成熟、使用不便且缺乏价值的产品。产品失败的原因多种多样,我将从不同角度深入分析。然而,我深信,根本问题在于公司对产品…

【Selenium】隐藏元素的定位和操作|隐藏与isDisplay方法

一、selenium 中隐藏元素如何定位&#xff1f; 如果单纯的定位的话&#xff0c;隐藏元素和普通不隐藏元素定位没啥区别&#xff0c;用正常定位方法就行了 但是吧~~~能定位到并不意味着能操作元素&#xff08;如click,clear,send_keys&#xff09; 二、隐藏元素 如下图有个输入框…

Alibaba spring cloud Dubbo使用(基于Zookeeper或者基于Nacos+泛化调用完整代码一键启动)

Quick Start Dubbo&#xff01;用更优雅的方式来实现RPC调用吧 - 掘金 dubbozookeeper demo 项目结构&#xff1a; RpcService 仅仅是提供服务的接口&#xff1a; public interface HelloService {String sayHello(String name); }DubboServer pom&#xff1a; <?xm…

EDR下的线程安全

文章目录 前记进程断链回调执行纤程内存属性修改early birdMapping后记reference 前记 触发EDR远程线程扫描关键api&#xff1a;createprocess、createremotethread、void&#xff08;指针&#xff09;、createthread 为了更加的opsec&#xff0c;尽量采取别的方式执行恶意代…

【Flutter学习笔记】10.3 组合实例:TurnBox

参考资料&#xff1a;《Flutter实战第二版》 10.3 组合实例&#xff1a;TurnBox 这里尝试实现一个更为复杂的例子&#xff0c;其能够旋转子组件。Flutter中的RotatedBox可以旋转子组件&#xff0c;但是它有两个缺点&#xff1a; 一是只能将其子节点以90度的倍数旋转二是当旋转…

2002-2023年各地级市环境规制强度数据(环保词频统计)

2002-2023年各地级市环境规制强度数据&#xff08;环保词频统计&#xff09; 1、时间&#xff1a;2002-2023年 2、来源&#xff1a;政府工作报告 3、指标&#xff1a; 行政区划代码、年份、城市、所属省份、文本总长度、仅中英文-文本总长度、文本总词频-全模式、文本总词频…

力扣刷题之22.括号生成

仅做学习笔记之用。 题目&#xff1a; 数字 n 代表生成括号的对数&#xff0c;请你设计一个函数&#xff0c;用于能够生成所有可能的并且 有效的 括号组合。 示例 1&#xff1a; 输入&#xff1a;n 3 输出&#xff1a;["((()))","(()())","(())(…

瑞_23种设计模式_职责链模式

文章目录 1 责任链模式&#xff08;Chain of Responsibility Pattern&#xff09;★★★1.1 介绍1.2 概述1.3 职责链模式的结构1.4 职责链模式的优缺点1.5 职责链模式的使用场景 2 案例一2.1 需求2.2 代码实现 3 案例二3.1 需求3.2 代码实现 4 JDK源码解析&#xff08;FilterCh…

软件测试 - postman高级使用

断言 概念&#xff1a;让程序代替人判断测试用例执行的结果是否符合预期的一个过程 特点&#xff1a; postman断言使用js编写&#xff0c;断言写在postman的tests中 tests脚本在发送请求之后执行&#xff0c;会把断言的结果最终在testresult中进行展示 常用的postman提供的…

C++剑指offer与高频面试题源码解答与分析

这是博主在当初秋招刷题时候记录的剑指offer第二版以及一些高频题的C源码和解法分析&#xff0c;可以说把这上面的题练好了面试不虚&#xff0c;最后也顺利帮助我拿下baidu ali meituan等多家大厂offer。整篇文章写了大概5W个字&#xff0c;也是积累了很长一段时间的作品&#…

【Docker】docker和docker-compose一键安装脚本(linux)

一、准备和运行脚本 当前脚本下载的docker和docker-compose兼容系统架构为x64&#xff0c;可以根据自己实际系统版本更改下载链接 1. 在控制台使用vim新建: vim install-docker.sh2. 复制内容并粘贴&#xff1a; #!/usr/bin/env bash # 设置脚本在遇到错误时终止执行 set -…