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

昆明网站建设c3sales网站后台都有哪些

昆明网站建设c3sales,网站后台都有哪些,有哪些做搞笑视频的网站,企业服务方案回溯算法 Part02 组合总数 力扣题目链接 代码随想录链接 视频讲解 题目描述: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你…

回溯算法 Part02

组合总数

力扣题目链接
代码随想录链接
视频讲解
题目描述: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

在这里插入图片描述

思路,本题的解法和part01的组合总和3的思路差别不大,其解树如下:
在这里插入图片描述

唯一的区别在于本题目之中的元素是可以重复选取的,因此,在递归传递参数的时候,我们应该传递i而不是i+1;

回溯法

具体代码如下:

class Solution {int sum = 0;List<Integer> path = new ArrayList<>();List<List<Integer>> result = new ArrayList<>(); public List<List<Integer>> combinationSum(int[] candidates, int target) {backTracking(candidates,target,0);return result;}private void backTracking(int[] candidates , int target , int startIndex){// 剪枝操作if(sum > target) return ;if(startIndex > candidates.length) return ;// 满足条件的情况存储if(sum == target){result.add(new ArrayList<>(path));return ;}for(int i = startIndex ; i < candidates.length ; i++){sum += candidates[i];path.add(candidates[i]);// 递归backTracking(candidates,target,i);// 回溯sum -= candidates[i];path.removeLast();}}
}

组合总数2

力扣题目链接
代码随想录链接
视频讲解
题目描述: 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。

在这里插入图片描述

思路:因为给的candidates 之中包含重复的元素,所以可能造成解集之中也包含同样的输出,但题目之中要求解集不能包含重复的组合。本题的解树为:
在这里插入图片描述

回溯法

思路:

class Solution {LinkedList<Integer> path = new LinkedList<>();List<List<Integer>> ans = new ArrayList<>();boolean[] used;int sum = 0;public List<List<Integer>> combinationSum2(int[] candidates, int target) {used = new boolean[candidates.length];// 加标志数组,用来辅助判断同层节点是否已经遍历Arrays.fill(used, false);// 为了将重复的数字都放到一起,所以先进行排序Arrays.sort(candidates);backTracking(candidates, target, 0);return ans;}private void backTracking(int[] candidates, int target, int startIndex) {// if(sum > target) return ;if (sum == target) {ans.add(new ArrayList(path));}for (int i = startIndex; i < candidates.length; i++) {if (sum + candidates[i] > target) {break;}// 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {continue;}used[i] = true;sum += candidates[i];path.add(candidates[i]);// 每个节点仅能选择一次,所以从下一位开始backTracking(candidates, target, i + 1);used[i] = false;sum -= candidates[i];path.removeLast();}}
}

回溯法(我的解决)

使用contains判断当前的结果path是否存在于result之中,不在则加入(会出现一些例子执行时间超出限制)

代码如下:

class Solution {int sum = 0 ;List<Integer> path = new ArrayList<>();List<List<Integer>> result = new ArrayList<>();public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);backTracking(candidates,target,0);return result;}private void backTracking(int[] candidates , int target , int startIndex){if(sum > target) return ;if(sum == target && !result.contains(path)){result.add(new ArrayList<>(path));return ;}for(int i = startIndex ; i < candidates.length ; i++){sum += candidates[i];path.add(candidates[i]);backTracking(candidates,target,i + 1);sum -= candidates[i];path.remove(path.size() - 1);}}}

分割回文串 (待补充)

力扣题目链接
代码随想录链接
视频讲解
题目描述:

回溯法

代码如下:

class Solution {//保持前几题一贯的格式, initializationList<List<String>> res = new ArrayList<>();List<String> cur = new ArrayList<>();public List<List<String>> partition(String s) {backtracking(s, 0, new StringBuilder());return res;}private void backtracking(String s, int start, StringBuilder sb){//因为是起始位置一个一个加的,所以结束时start一定等于s.length,因为进入backtracking时一定末尾也是回文,所以cur是满足条件的if (start == s.length()){//注意创建一个新的copyres.add(new ArrayList<>(cur));return;}//像前两题一样从前往后搜索,如果发现回文,进入backtracking,起始位置后移一位,循环结束照例移除cur的末位for (int i = start; i < s.length(); i++){sb.append(s.charAt(i));if (check(sb)){cur.add(sb.toString());backtracking(s, i + 1, new StringBuilder());cur.remove(cur.size() -1 );}}}//helper method, 检查是否是回文private boolean check(StringBuilder sb){for (int i = 0; i < sb.length()/ 2; i++){if (sb.charAt(i) != sb.charAt(sb.length() - 1 - i)){return false;}}return true;}
}


文章转载自:

http://5fLaPlKd.nfxps.cn
http://5bxK1bXI.nfxps.cn
http://kIA9kn2z.nfxps.cn
http://QLyO0cVe.nfxps.cn
http://SQJpPKzR.nfxps.cn
http://ZnasMcvH.nfxps.cn
http://nqnJGxaG.nfxps.cn
http://Va8pD2dz.nfxps.cn
http://be9YyiQl.nfxps.cn
http://FLLag0hv.nfxps.cn
http://Fna1sB0w.nfxps.cn
http://RvoHHBgk.nfxps.cn
http://gUkIc3XD.nfxps.cn
http://tCxKBp4D.nfxps.cn
http://r5MLN5JY.nfxps.cn
http://7wR1Zff1.nfxps.cn
http://gUcckELy.nfxps.cn
http://muhDhAhL.nfxps.cn
http://WSLa5uj2.nfxps.cn
http://OS9OUJ7g.nfxps.cn
http://9QEjA1y2.nfxps.cn
http://UickhKLM.nfxps.cn
http://ioAbAaMX.nfxps.cn
http://xq5uCUxz.nfxps.cn
http://EXfWgBV3.nfxps.cn
http://JX1BX0DN.nfxps.cn
http://5LlTXIuw.nfxps.cn
http://VyLzSJeO.nfxps.cn
http://4ue63MCA.nfxps.cn
http://dwxXqUDw.nfxps.cn
http://www.dtcms.com/wzjs/686115.html

相关文章:

  • 深圳 网站建设 公司什么叫网络服务商
  • 网站后台数字排版该怎么做网站开发实用技术2.8.5
  • 白山网站建设贵州省建设工程造价信息网
  • 谷歌网站推广好做吗做个网站多少钱怎么接广告
  • 深圳高端网站定制公市场营销网络
  • 企业做网站的注意什么宁波公司网站建设价格
  • 建设信用卡购物网站简单的网站建设企业
  • 深圳网站建设公司联华wordpress企业主体
  • 网页设计教程免费网站自己做小程序要钱吗
  • 搜狐快站做网站教程特效网站大全
  • 做网站办贷款怎么开发一个微信商城
  • 手机网站弹出层插件有哪些怎么自己在百度上做网站
  • 微信后台网站建设天津网址
  • 广州推广网站公众号制作多少钱
  • 网站后台ftp在哪七牛云上市
  • 电影网站建设的程序西安网站制作一般多少钱
  • 去泰国做网站发网站wordpress 标签显示
  • 免费网站源码...职参简历网站
  • 做精神科网站价格网站开发工具怎么改内容
  • 免费网站推广软件做企业网站需要做什么
  • python做的网站多吗台州网站优化
  • 公司企业网站程序下载宁夏企业网站建设
  • 网站建设如何控标深圳市宝安区西乡街道邮政编码
  • 网站推广 网站成都旅游几月份最佳时间
  • 有什么做衣服的网站好如何进入网站
  • 深圳建设手机网站wordpress中文版和英文版区别
  • 优质网站建设在哪里市场营销案例100例及答案
  • wordpress 网站提速德州手机网站建设
  • 茶叶公司网站源码国外网站建设软件排行榜
  • 成品网站货源wordpress怎么设置友情链接