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

可以做数学题的网站乔拓云智能建站

可以做数学题的网站,乔拓云智能建站,做独立网站的好处,ipad做网站服务器#LeetCode 491. Non-decreasing Subsequences #LeetCode 491. 视频讲解:回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列_哔哩哔哩_bilibili 首先,本题不能考虑首先对数组排序,排序会导致数组直接变为一个…

#LeetCode 491. Non-decreasing Subsequences

#LeetCode 491. 视频讲解:回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列_哔哩哔哩_bilibili

首先,本题不能考虑首先对数组排序,排序会导致数组直接变为一个递增的序列。本题依然是一个组合问题,所以无序,i 在for loop中依然是从startIndex 开始。与之前的去重逻辑不同,每一层会有一个uset 变量,是记录本层元素是否重复使用,新的一层uset都会重新定义,不是之前去重那样的全局变量,同样回溯的时候也不需要删除元素,也是在树层去重。

代码:

class Solution {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> findSubsequences(int[] nums) {backtracking(nums, 0);return result;}public void backtracking(int[] nums, int startIndex) {if (startIndex > nums.length) {return;}if (path.size() > 1) {result.add(new ArrayList<>(path));}HashSet<Integer> uset = new HashSet<>();for (int i = startIndex; i < nums.length; i++) {if (!path.isEmpty() && path.getLast() > nums[i] || uset.contains(nums[i])) {continue;}uset.add(nums[i]);path.addLast(nums[i]);backtracking(nums, i + 1);path.removeLast();}}
}

#LeetCode 46. Permutations

#LeetCode 46. 视频讲解:组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列_哔哩哔哩_bilibili

与之前的组合问题不同的地方在于,排列是有序的,[1, 2] 和[2, 1] 是不同的,如果用used 数组,那么在for loop 遍历的时候也是通过used 数组的标记,来判断下一次取具体哪一个数字。

回溯方法代码:

class Solution {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> permute(int[] nums) {boolean[] used = new boolean[nums.length];Arrays.fill(used, false);backtracking(nums, used);return result;}public void backtracking(int[] nums, boolean[] used) {if (path.size() == nums.length) {result.add(new LinkedList<>(path));return;}for (int i = 0; i < nums.length; i++) {if (used[i]) {continue;}used[i] = true;path.addLast(nums[i]);backtracking(nums, used);used[i] = false;path.removeLast();}}
}

#LeetCode 47. Permutations II

#LeetCode 47. 视频讲解:回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II_哔哩哔哩_bilibili

如果完成了之前的题目,那么这个题目较简单,排列的思想与上一个题目相似,去重与之前组合去重相似,记得数组需要排序。

回溯方法代码:

class Solution {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> permuteUnique(int[] nums) {boolean[] used = new boolean[nums.length];Arrays.fill(used, false);Arrays.sort(nums);backtracking(nums, used);return result;}public void backtracking(int[] nums, boolean[] used) {if (path.size() == nums.length) {result.add(new LinkedList<>(path));return;}for (int i = 0; i < nums.length; i++) {if (i > 0 && nums[i] == nums[i-1] && !used[i-1]) {continue;}if (used[i]) {continue;}path.addLast(nums[i]);used[i] = true;backtracking(nums, used);used[i] = false;path.removeLast();}}
}
http://www.khdw.cn/news/64068.html

相关文章:

  • 郓城建设局网站建立营销型网站
  • 衡水住房和城乡建设局网站网页优化方法
  • asp网站攻击百度公司总部在哪里
  • 政府高度重视门户网站建设的理由百度推广app下载
  • 做包装盒子的厂家哪个网站关键词排名查询官网
  • 杜桥做网站哪家好阿拉营销网站
  • 做网站需要审核资质吗推广优化网站
  • 白云区新闻360优化大师官方版
  • 学生个人网页设计作品代码黑锋网seo
  • 兼职python做网站成人本科报考官网
  • 优化网站内容的方法私人网站管理软件
  • 微信开发者工具是干嘛的博客seo怎么做
  • 高校网站建设汇报宁波seo搜索排名优化
  • phpstudy网站端口广州各区最新动态
  • 石家庄有哪些做网站的公司站牛网是做什么的
  • 暖暖视频 高清 日本北京seo招聘
  • 应用软件开发工程师是做什么的小红书seo是什么意思
  • 2019年新电商法做网站平台网站开发公司
  • 长沙做公司网站大概多少钱阿里云自助建站
  • 单页加盟网站模板好看的web网页
  • 免费的微信小程序模板常用的关键词优化策略有哪些
  • 网站设置为起始页贵州二级站seo整站优化排名
  • seo专业优化方法seo黑帽有哪些技术
  • 威海做企业网站的公司免费推广链接
  • wordpress登录cdn宁波seo推广
  • 沈阳做机床的公司网站一站式网站建设
  • php 上传移动到网站根目录北京优化网站公司
  • 手机网站一键分享到微信线上引流线下推广方案
  • 青岛大型网站设计公司系列推广软文范例
  • 我想去澳大利亚做按摩找哪个网站百度精准推广