当前位置: 首页 > news >正文

回溯算法part05 算法

回溯算法part05 算法

今日任务

  • 491.递增子序列
  • 46.全排列
  • 47.全排列 II

1.LeetCode 491.递增子序列

https://leetcode.cn/problems/non-decreasing-subsequences/description/

class Solution {List<List<Integer>> result=new ArrayList<>();List<Integer> path=new ArrayList<>();public List<List<Integer>> findSubsequences(int[] nums) {backtracing(nums,0);return result;}public void backtracing(int[] nums,int startIndex){if(path.size()>=2){//一维加进2维的,究竟符不符合条件在单层那里处理result.add(new ArrayList<>(path));}HashSet<Integer> set=new HashSet<>();for(int i=startIndex;i<nums.length;i++){if(!path.isEmpty()&&path.get(path.size()-1)>nums[i]||set.contains(nums[i])){//这是处理横向的,没错//一维不为空&&一维集合最后一个元素值>现在遍历到的新元素值||hashset包含着现在遍历到的新元素值,证明是同一父节点下的同层上使用过的元素//第二个可以是[4,1,2,5],=>[4,5]//第三个同一父节点下的同层上使用过的元素就不能再使用了,因为这是处理横向的//[4,1,2,5,5],[4,5] [4,5]这样就重复了,当然可以纵向,但是要避免横向不能重复continue;}//为空set.add(nums[i]);path.add(nums[i]);backtracing(nums,i+1);path.removeLast();}}
}

2.LeetCode 46.全排列

https://leetcode.cn/problems/permutations/

class Solution {List<List<Integer>> result=new ArrayList<>();List<Integer> path=new ArrayList<>();int[] used;//初始值默认都是0public List<List<Integer>> permute(int[] nums) {used=new int[nums.length];//初始值默认都是0backtracing(nums);return result;}public void backtracing(int[] nums){//终止条件,是到叶子节点时if(path.size()==nums.length){result.add(new ArrayList<>(path));return;}for(int i=0;i<nums.length;i++){//已经有这个来控制横向了//如果当前值没有被用过if(used[i]==1){continue;}path.add(nums[i]);//标记为已经用过used[i]=1;backtracing(nums);//深层的已经用完要变化used[i]=0;path.removeLast();}}
}

3.LeetCode 47.全排列 II

https://leetcode.cn/problems/permutations-ii/description/

//跟前一题有什么区别吗
//元素我重复了又如何,我是按照位置改的,又不是按照元素值
//事实证明,有区别,会出现重复的一维集合,需要过滤
class Solution {List<List<Integer>> result=new ArrayList<>();List<Integer> path=new ArrayList<>();int[] used;//初始值默认都是0public List<List<Integer>> permuteUnique(int[] nums) {used=new int[nums.length];//初始值默认都是0backtracing(nums);return result;}public void backtracing(int[] nums){//终止条件,是到叶子节点时if(path.size()==nums.length){result.add(new ArrayList<>(path));return;}HashSet<Integer> set=new HashSet<>();for(int i=0;i<nums.length;i++){//已经有这个来控制横向了//如果当前值没有被用过if(used[i]==1||set.contains(nums[i])){continue;}set.add(nums[i]);path.add(nums[i]);//标记为已经用过used[i]=1;backtracing(nums);//深层的已经用完要变化used[i]=0;path.removeLast();}}
}
http://www.lryc.cn/news/277626.html

相关文章:

  • 阿里云系统盘测评ESSD、SSD和高效云盘IOPS、吞吐量性能参数表
  • RK3568平台开发系列讲解(Linux系统篇)Linux 内核打印
  • 迁移学习的最新进展和挑战
  • Python基础(二十二、自定义模块和包)
  • C#-数组
  • 机器学习周刊第二期:300个机器学习应用案例集
  • 【华为OD机试真题2023CD卷 JAVAJS】中文分词模拟器
  • 基于YOLOv8-pose的画笔关键点(bic_markers)检测
  • 【实用技巧】Windows 电脑向iPhone或iPad传输视频方法1:无线传输
  • 爬虫实战 - 微博评论数据可视化
  • python装饰器嵌套基础
  • C语言之三子棋小游戏的应用
  • 优雅处理并发:Java CompletableFuture最佳实践
  • 熟悉HDFS常用操作
  • Adobe XD是什么?探索这款创新的用户体验设计工具
  • java常用应用程序编程接口(API)——ArrayList概述及使用案例
  • 2024年了,Layui再战三年有问题不?
  • 消息队列-RocketMQ-概览与搭建
  • Vue3技术解析(小册子)
  • 即将消失的五种编程语言?
  • c++学习:STL库(框架)+字符串模板类string+vector容器+list链表
  • 2023年全国职业院校技能大赛(高职组)“云计算应用”赛项赛卷④
  • 使用Scikit Learn 进行识别手写数字
  • GB/T 15036-2018 实木地板检测
  • 基于ElementUI封装的下拉树选择可搜索单选多选清空功能
  • 计算机网络-各层协议
  • LeetCode 84:柱状图中的最大矩形
  • 老生重谈:大模型的「幻觉」问题
  • golang实现skiplist 跳表
  • 尝试OmniverseFarm的最基础操作