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

义乌 网站建设格格导航官网

义乌 网站建设,格格导航官网,盈利的网站,a96中华室内设计网文章目录 39.组合总和40.组合总和II131.分割回文串 39.组合总和 文字讲解&#xff1a;组合总和 视频讲解&#xff1a;组合总和 状态: 此题ok 思路&#xff1a; 代码&#xff1a; class Solution {int sum;public List<List<Integer>> combinationSum(int[] candi…

文章目录

    • 39.组合总和
    • 40.组合总和II
    • 131.分割回文串

39.组合总和

文字讲解:组合总和

视频讲解:组合总和

状态: 此题ok

思路:

代码:

class Solution {int sum;public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();backTracking(result, tempList, candidates, 0, target);return result;}public void backTracking(List<List<Integer>> result, LinkedList<Integer> tempList, int[] candidates, int startIndex, int target) {if (!tempList.isEmpty()&&sum>target) {return;}if (!tempList.isEmpty() && sum==target) { //收集List<Integer> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < candidates.length; i++) {tempList.offer(candidates[i]);sum+=candidates[i];backTracking(result, tempList, candidates, i, target);tempList.pollLast();sum-=candidates[i];}}
}

40.组合总和II

文字讲解:组合总和II

视频讲解:组合总和II

状态:这题一开始想先排序后去重,再做回溯;想岔批了,应该要保证前后处理相同元素的时候,要进行跳过来进行去重;

思路:

1、这一题,注意枝剪操作,即sum + candidates[i] <= target;不然提交leetcode会超时;

代码:

class Solution {int sum;public List<List<Integer>> combinationSum2(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();Arrays.sort(candidates);//完成去重backTracking(result, tempList, candidates, 0, target);return result;}public void backTracking(List<List<Integer>> result, LinkedList<Integer> tempList, int[] candidates, int startIndex, int target) {if (!tempList.isEmpty()&&sum==target) {List<Integer> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < candidates.length && sum + candidates[i] <= target; i++) {if (i>startIndex && candidates[i]==candidates[i-1]) {continue;}tempList.offer(candidates[i]);sum+=candidates[i];backTracking(result, tempList, candidates, i+1, target);tempList.pollLast();sum-=candidates[i];}}
}

131.分割回文串

文字讲解:分割回文串

视频讲解:分割回文串

状态:这题细细一想,和前两题也是类似换汤不换药,看来还是对回溯-排列问题理解不够透彻;

思路:

代码:

class Solution {List<List<String>> result = new ArrayList<>();LinkedList<String> tempList = new LinkedList<>();public List<List<String>> partition(String s) {backTracking(s, 0);return result;}public void backTracking(String s, int startIndex) {if (startIndex>=s.length()) {List<String> resultParam = new ArrayList<>(tempList);result.add(resultParam);return;}for (int i = startIndex; i < s.length(); i++) {if(isPalindrome(s, startIndex, i)) {tempList.offer(s.substring(startIndex, i+1));} else {continue;}backTracking(s, i+1);tempList.pollLast();}}public boolean isPalindrome(String s, int start, int end) {while (start<=end) {if (s.charAt(start) == s.charAt(end)) {start++;end--;} else {return false;}}return true;}
}
http://www.yayakq.cn/news/776713/

相关文章:

  • 珠海市网站开发公司电话永康网站开发公司
  • 网站开发市场商丘 峰少 seo博客
  • 商会网站制作wordpress无法发送注册邮件
  • 做个网站哪里可以做定制网站建设服务公司
  • 注册个人网站域名top锦州网站建设新闻
  • 网站用途百度网站打开
  • 怎么做领券网站网站建设那里好
  • o2o医药电商平台有哪些南安seo教程
  • 洛阳微网站建设查查企业网
  • 湖北网站遵义网站制作费用
  • 有没有做西餐的视频网站h5网站开发框架
  • 怎样用织梦建设网站aspcms 网站栏目管理
  • 梁园区官方网站 天堂资源帝
  • 绿色手机网站模板wordpress文章上传视频
  • 一个网站的作用是什么如何免费制作一个公司网站
  • 网站结构流程图怎么做邢台视频推广
  • 网站开发 技术维护wordpress 的环境搭建
  • 密云区社区建设网站网站备案归
  • 网页设计与网站建设ppt我做网站推广
  • 泰安做网站哪里好wordpress 跳转适配
  • 网站备案主体信息变更平乡县网站建设
  • 申请建设网站请示网站制作软件手机版下载
  • 沧州国外网站建设wordpress二级域名设置
  • 关于网站建设的书籍商业设计网站推荐
  • 求网站都懂得常州市武进区城乡建设局网站
  • 项目建设环境影响登记表在哪个网站店铺店面装修
  • 电影网站开发库表结构个人网站做什么好
  • 安徽建设工程安全监督网站wordpress 3.3.1 漏洞
  • 营销型企业网站建设方案外贸网站建设科技
  • 安阳中飞网站建设优化排名 生客seo