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

房地产网站建设与优化分析山东seo推广公司

房地产网站建设与优化分析,山东seo推广公司,商丘做网站推广的公司,男女做那个网站动态图片39. 组合总和 39. 组合总和 - 力扣(LeetCode) 题目链接/文章讲解:代码随想录 视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)| 回溯法精讲!_哔哩哔哩_…

39. 组合总和

39. 组合总和 - 力扣(LeetCode)

题目链接/文章讲解:代码随想录

视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)| 回溯法精讲!_哔哩哔哩_bilibili

自己乱写的,回溯的时候没有去掉重复使用的数字

class Solution {List<Integer> group = new ArrayList<>();List<List<Integer>> result = new ArrayList<>();int sum = 0;public void backTracking (int[] candidates,int target){if(sum == target){List<Integer> temp = new ArrayList(group);Collections.sort(temp);for(List<Integer> oneList:result){if(oneList.equals(temp)==true)return;}result.add(new ArrayList(temp));return;}else if(sum > target)return;for(int i = 0;i < candidates.length;i++){group.add(candidates[i]);sum += candidates[i];backTracking(candidates,target);group.remove(group.size() - 1);sum -= candidates[i];}}public List<List<Integer>> combinationSum(int[] candidates, int target) {backTracking(candidates,target);return result;}
}

没有剪枝

剪枝

修改

class Solution {List<Integer> group = new ArrayList<>();List<List<Integer>> result = new ArrayList<>();int sum = 0;public void backTracking (int[] candidates,int target,int startIndex){if(sum == target){result.add(new ArrayList(group));return;}for(int i = startIndex;i < candidates.length;i++){if(candidates[i] + sum > target)break;//排过序,现在已经超过target,说明后面的数已经不需要遍历了group.add(candidates[i]);sum += candidates[i];backTracking(candidates,target,i);group.remove(group.size() - 1);sum -= candidates[i];}}public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);//一定要先对数组排序,为了剪枝backTracking(candidates,target,0);return result;}
}

40.组合总和II

40. 组合总和 II - 力扣(LeetCode)

注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。

题目链接/文章讲解:代码随想录

视频讲解:​​​​​​回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili

class Solution {List<Integer> path = new ArrayList<>();List<List<Integer>> result = new ArrayList<>();public void backTracking(int[] candidates,int target,int startIndex,int sum,boolean[] used){if(sum == target){result.add(new ArrayList(path));return;}else if(sum > target)return;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] == false){//去重操作continue;}path.add(candidates[i]);sum += candidates[i];used[i] = true;backTracking(candidates,target,i + 1,sum,used);//下一层递归path.remove(path.size() - 1);//回溯sum -= candidates[i];used[i] = false;}}public List<List<Integer>> combinationSum2(int[] candidates, int target) {boolean[] used = new boolean[candidates.length];//记录元素是否被使用Arrays.sort(candidates);//必须排序backTracking(candidates,target,0,0,used);return result;}
}

131.分割回文串

131. 分割回文串 - 力扣(LeetCode)

本题较难,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。

代码随想录

视频讲解:带你学透回溯算法-分割回文串(对应力扣题目:131.分割回文串)| 回溯法精讲!_哔哩哔哩_bilibili

class Solution {List<String> path = new ArrayList<>();List<List<String>> result = new ArrayList<>();public boolean huiwen(String s,int start,int end){while(start < end){if(s.charAt(start) != s.charAt(end))return false;start++;end--;}return true;}public void backTracking(String s,int startIndex){if(startIndex == s.length()){result.add(new ArrayList(path));return;}for(int i = startIndex;i < s.length();i++){if(huiwen(s,startIndex,i)){//左闭右闭区间path.add(s.substring(startIndex,i + 1));//已经切过的不能再切}else continue;backTracking(s,i + 1);path.remove(path.size()-1);}}public List<List<String>> partition(String s) {backTracking(s,0);return result;} 
}

http://www.dtcms.com/wzjs/386666.html

相关文章:

  • 百度公司网站建设网络优化工程师是干什么的
  • 短视频seo公司指定关键词排名优化
  • 网站开发什么语音最好2023年国际新闻大事件10条
  • 网站制作的步骤不包括哪些网站建设的意义和目的
  • 竞价培训班seo培训
  • 信息类网站怎么做网络营销推广手段
  • 做网站要学什么知识电销精准客户资源
  • 怎样建立自己的网站卖东西百度seo关键词优化排行
  • 网站模板 素材java培训学费多少钱
  • 镇江网站建设一般多少钱线上营销活动有哪些
  • 兼职网站制作114黄页
  • 如何建网站卖东西上海专业排名优化公司
  • 网站建设中 图片推广普通话ppt课件
  • 网上购物平台类型有哪些企业网站seo推广方案
  • photoshop网站模板设计教程视频seo优化专员工作内容
  • 自己创建网站容易吗天津百度网站排名优化
  • 做兼职工作上哪个网站招聘网站seo方案
  • 民宅挂在民宿网站上 保洁谁做开封seo推广
  • 北京网站制作公司有哪些营销与销售的区别
  • 石家庄公司的网站设计网店运营与推广
  • mac上用wordpress网站建设推广优化
  • 成都网页制作设计全国最好网络优化公司
  • 网站共享备案可以申请支付接口商丘seo博客
  • wordpress价格比较模板北京seo供应商
  • 中山古镇做网站的公司关键词排名优化公司哪家强
  • 微网站设置免费网络营销方式
  • 能看任何网站的浏览器seo外链软件
  • 葫芦岛市住房和城乡建设局网站前端培训班一般多少钱
  • 集团公司网站怎么做网站被百度收录
  • 图片展示 网站公司官网模板