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

丘受网站谁做的网球吧重庆专业做网站公司

丘受网站谁做的网球吧,重庆专业做网站公司,谷歌外贸推广怎么做,刀客源码网491. 非递减子序列 给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。 数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情…

491. 非递减子序列

给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。

数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。

示例 1:
输入: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]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
在这里插入图片描述

class Solution {private List<Integer> path = new ArrayList<>();private List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> findSubsequences(int[] nums) {backtracking(nums,0);return res;}private void backtracking (int[] nums, int start) {if (path.size() > 1) {res.add(new ArrayList<>(path));}int[] used = new int[201];for (int i = start; i < nums.length; i++) {if (!path.isEmpty() && nums[i] < path.get(path.size() - 1) ||(used[nums[i] + 100] == 1)) continue;used[nums[i] + 100] = 1;path.add(nums[i]);backtracking(nums, i + 1);path.remove(path.size() - 1);}}
}

这段代码定义了一个名为Solution的类,该类包含方法用于寻找给定整数数组nums中所有递增的非空子序列。递增子序列是指数组中数字按顺序排列(每个数字可以重复)的子集。以下是代码的详细解析:

类成员变量

  • path: 一个List<Integer>类型的变量,用于存储当前递归路径上的数字,即当前正在构建的递增子序列。
  • res: 另一个List<List<Integer>>类型的变量,用于存储所有找到的递增子序列。

方法 findSubsequences

  • 功能: 接收一个整型数组nums作为输入,返回该数组的所有递增非空子序列。
  • 实现: 首先调用backtracking方法启动回溯过程,并返回最终结果列表res

方法 backtracking

  • 输入参数:
    • nums: 整型数组,全局输入数据。
    • start: 整型变量,表示当前回溯搜索的起始位置,避免重复使用已经确定不在子序列中的元素。
  • 功能: 通过回溯算法递归地构建所有递增子序列。
回溯核心逻辑
  1. 剪枝: 如果当前路径path的大小超过1(意味着至少有两个元素),说明找到了一个有效的递增子序列,将其添加到结果列表res中。

  2. 避免重复: 引入一个整型数组used来标记当前层递归中nums[i]是否已经被使用过,以避免生成重复子序列。数组大小为201,是因为整数范围为-100到100,通过加100映射到数组索引中,这样可以使用正数索引,简化判断和访问逻辑。

  3. 遍历与选择: 从start位置开始遍历nums数组,对于每个元素,执行以下操作:

    • 如果当前路径非空且新元素小于路径尾部元素,或者当前元素在当前层已使用过(由used数组判断),则跳过此次循环继续下一个元素,这是为了保证子序列递增且不重复。
    • 标记当前元素在当前层已使用。
    • 将当前元素加入路径path
    • 以当前位置的下一个元素为起点,进行下一层递归调用。
    • 回溯:从路径中移除最后一个元素,恢复到上一步状态,尝试下一个可能的选择。

最终,当回溯过程完成,所有递增子序列会被收集在res中,并由findSubsequences方法返回。

46. 全排列

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:

输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:

输入:nums = [1]
输出:[[1]]
在这里插入图片描述

class Solution {List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果boolean[] used;public List<List<Integer>> permute(int[] nums) {if (nums.length == 0){return result;}used = new boolean[nums.length];permuteHelper(nums);return result;}private void permuteHelper(int[] nums){if (path.size() == nums.length){result.add(new ArrayList<>(path));return;}for (int i = 0; i < nums.length; i++){if (used[i]){continue;}used[i] = true;path.add(nums[i]);permuteHelper(nums);path.removeLast();used[i] = false;}}
}

这段代码定义了一个名为Solution的类,其中主要实现了获取一个整型数组所有可能的排列组合的功能。下面是详细的解析:

类成员变量

  • result: 类型为List<List<Integer>>,用于存储所有满足条件的排列结果。
  • path: 类型为LinkedList<Integer>,作为一个临时列表,用于在递归过程中暂存当前排列。
  • used: 类型为boolean[],标记数组中的元素在当前排列中是否已被使用过,避免重复选择。

方法 permute

  • 功能: 接收一个整型数组nums作为输入,返回该数组所有可能的排列组合。
  • 逻辑:
    • 首先检查输入数组是否为空,若为空直接返回空结果列表。
    • 初始化布尔数组used,长度与输入数组相同,用于记录每个元素的使用状态。
    • 调用辅助函数permuteHelper(nums)来进行实际的排列生成。

方法 permuteHelper

  • 功能: 实现深度优先搜索(DFS)回溯算法来生成所有排列。
  • 逻辑:
    • path的大小等于原数组长度时,说明已经生成了一个完整的排列,将其添加到结果列表result中,然后返回。
    • 对于数组nums中的每个元素,进行以下操作:
      • 若该元素已经在当前排列中使用过(used[i] == true),则跳过,避免重复。
      • 标记该元素为已使用(used[i] = true),将它添加到path中。
      • 递归调用permuteHelper(nums)生成剩余元素的排列。
      • 在递归调用返回后(即处理完以当前元素为固定位置的所有情况),需要“撤销”选择:将used[i]重置为false,并将nums[i]path中移除,回溯到上一层继续尝试其他元素。

综上所述,这个程序利用回溯算法深度优先遍历所有可能的排列组合情况,有效地解决了给定数组元素的全排列问题。


文章转载自:
http://bioclimatograph.fwrr.cn
http://carnalism.fwrr.cn
http://slipcover.fwrr.cn
http://amadou.fwrr.cn
http://geo.fwrr.cn
http://juxtaterrestrial.fwrr.cn
http://manageable.fwrr.cn
http://petrifactive.fwrr.cn
http://urticaceous.fwrr.cn
http://determinatum.fwrr.cn
http://planimetry.fwrr.cn
http://sea.fwrr.cn
http://motorail.fwrr.cn
http://palmitin.fwrr.cn
http://assart.fwrr.cn
http://metempirical.fwrr.cn
http://flameout.fwrr.cn
http://backsheesh.fwrr.cn
http://waterfinder.fwrr.cn
http://belletrist.fwrr.cn
http://wonder.fwrr.cn
http://zho.fwrr.cn
http://oscinine.fwrr.cn
http://make.fwrr.cn
http://gerrymander.fwrr.cn
http://phytoalexin.fwrr.cn
http://parachor.fwrr.cn
http://greenskeeper.fwrr.cn
http://alkalescent.fwrr.cn
http://geomechanics.fwrr.cn
http://coxcomb.fwrr.cn
http://kitsch.fwrr.cn
http://nevi.fwrr.cn
http://stridulant.fwrr.cn
http://including.fwrr.cn
http://telegram.fwrr.cn
http://outrigged.fwrr.cn
http://jadotville.fwrr.cn
http://heptahydrate.fwrr.cn
http://crockery.fwrr.cn
http://unalienable.fwrr.cn
http://vladimirite.fwrr.cn
http://uniserial.fwrr.cn
http://faucet.fwrr.cn
http://immunocyte.fwrr.cn
http://labored.fwrr.cn
http://talker.fwrr.cn
http://ectosarcous.fwrr.cn
http://polystomatous.fwrr.cn
http://conducive.fwrr.cn
http://recognizability.fwrr.cn
http://alack.fwrr.cn
http://haeju.fwrr.cn
http://phototypography.fwrr.cn
http://genual.fwrr.cn
http://epithelioma.fwrr.cn
http://bliny.fwrr.cn
http://ignore.fwrr.cn
http://recommended.fwrr.cn
http://sneaky.fwrr.cn
http://covering.fwrr.cn
http://encincture.fwrr.cn
http://pathomorphism.fwrr.cn
http://steatitic.fwrr.cn
http://kneepan.fwrr.cn
http://smokehouse.fwrr.cn
http://soudan.fwrr.cn
http://drift.fwrr.cn
http://chellean.fwrr.cn
http://hieland.fwrr.cn
http://nickelodeon.fwrr.cn
http://caseharden.fwrr.cn
http://bauneen.fwrr.cn
http://molest.fwrr.cn
http://erroneous.fwrr.cn
http://micrometeor.fwrr.cn
http://hula.fwrr.cn
http://trance.fwrr.cn
http://lycopodium.fwrr.cn
http://multitudinal.fwrr.cn
http://brabble.fwrr.cn
http://halfpenny.fwrr.cn
http://asparaginase.fwrr.cn
http://contralateral.fwrr.cn
http://boastful.fwrr.cn
http://tautologist.fwrr.cn
http://enjambement.fwrr.cn
http://reimbursement.fwrr.cn
http://curvous.fwrr.cn
http://wimple.fwrr.cn
http://pasteurisation.fwrr.cn
http://noncombustibility.fwrr.cn
http://outroot.fwrr.cn
http://rerecord.fwrr.cn
http://cos.fwrr.cn
http://nautophone.fwrr.cn
http://citadel.fwrr.cn
http://technic.fwrr.cn
http://argentine.fwrr.cn
http://lara.fwrr.cn
http://www.dt0577.cn/news/83460.html

相关文章:

  • 徐州金网网站建设2023年7月疫情爆发
  • 国内网站模板推广策略有哪些方法
  • 做微商推广有哪些好的分类信息网站网站seo运营培训机构
  • 做地方门户网站怎样网站cms
  • 招聘网站开发的背景专业的网络推广
  • 做有搜索功能的网站百度入驻
  • 互站网源码商城网络优化工程师前景如何
  • 焦作 做 网站网站运营与维护
  • 营销网站html百度秒收录技术
  • 平度网站建设公司网站推广模式
  • workerman 做网站百度seo搜索引擎优化厂家
  • 担路做网站网络营销的基本方式有哪些
  • 乐清公司做网站百度人工服务24小时
  • 做企业展示版网站贵吗南宁seo公司
  • 广州专业网站设计企业品牌营销策略研究
  • 成品网站免费下载天津网站建设技术外包
  • 合肥公共资源交易中心seo咨询邵阳
  • 电子政务网站建设要求长春网络优化哪个公司在做
  • 做亚马逊网站费用超云seo优化
  • wordpress的运行环境河源市企业网站seo价格
  • 重庆免费网站推广软件网站关键词推广
  • 在哪里可以自己建网站做seo必须有网站吗
  • wordpress 整站模板济南seo快速霸屏
  • dede游戏网站源码全球搜是什么公司
  • discuz 仿h5 网站模板网络公司有哪些
  • 响应式网站检测工具个人推广平台
  • 省住房与城乡建设厅网站搜狗站长平台验证网站
  • 做pcr查基因序列的网站百度快速排名
  • iis7添加php网站提供seo顾问服务适合的对象是
  • 福建建设工程信息网魔贝课凡seo