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

汝阳建设局网站网站更新维护

汝阳建设局网站,网站更新维护,宜昌网站制作公司亿腾,免费做网络推广Java Arrays.sort(数组) //排序 不讲究顺序的解答,都可以考虑一下排序是否可行。 39. 组合总和 错误解答 在写的时候需要注意,sum - candidates[i];很重要,也是回溯的一部分。 解答重复了。是因为回溯的for循环理解错了。 class Solutio…

Java

Arrays.sort(数组) //排序
不讲究顺序的解答,都可以考虑一下排序是否可行。

39. 组合总和

错误解答

在写的时候需要注意,sum -= candidates[i];很重要,也是回溯的一部分。
解答重复了。是因为回溯的for循环理解错了。

class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();public List<List<Integer>> combinationSum(int[] candidates, int target) {backtracking(candidates, target, 0, 0);return res;}List<Integer> path = new ArrayList<>();public void backtracking(int[] candidates, int target, int sum, int index) {if(sum > target) {return;}if(sum == target) {res.add(new ArrayList<>(path));return;}for(int i=0; i<candidates.length; i++) {sum += candidates[i];path.add(candidates[i]);backtracking(candidates,target,sum,i);sum -= candidates[i];path.remove(path.size()-1);}}
}

在这里插入图片描述

正确

  • 修改成下面这样就对了
    在这里插入图片描述

优化

不讲究顺序的解答,都可以考虑一下排序是否可行。
剪枝要先排序。

class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);backtracking(candidates, target, 0, 0);return res;}List<Integer> path = new ArrayList<>();public void backtracking(int[] candidates, int target, int sum, int index) {if(sum == target) {res.add(new ArrayList<>(path));return;}for(int i=index; i<candidates.length; i++) {sum += candidates[i];if (sum > target) break;path.add(candidates[i]);backtracking(candidates,target,sum,i);sum -= candidates[i];path.remove(path.size()-1);}}
}

40.组合总和II

想得太简单了……

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

131.分割回文串

http://www.yayakq.cn/news/877654/

相关文章:

  • app科技网站建设代做毕网站
  • 上海城市建设官方网站WordPress更改logo插件
  • 网站模板 寻模板找厂家用什么软件
  • 潍坊网站推广排名北京ui及网页设计
  • 自学网站建设视频项目vi设计公司
  • 公司网站建设需要考虑什么问题有关网站建设文章
  • 现在网站都是拿什么软件做的上海高端网站定
  • wordpress ss管理郑州seo技术服务顾问
  • 网站建设栏目结构表找人做网站协议
  • 兴业大街网站建设网站集约化建设 技术
  • 南宁市网站开发网络推广培训培训机构
  • 广东网站建设公司报价网站开发公司怎么查
  • 更换网站模板公众号开发需要提供什么
  • 建设银行网站查询工资纯流量卡免费申请入口
  • 单页网站域名网站如何设置关键词
  • 成都网站推广技巧2016网站优化
  • vps网站管理器东莞建设公司网页
  • 网站群方案今天的新闻直播
  • 深圳做微信网站网站支付宝接口代码
  • wordpress 关站百度一下你就知道官网网页
  • 个人怎么做音乐网站网站建设的方法有哪些方面
  • 竞价托管推广哪家好微信seo排名优化软件
  • 网站支付的功能如何做php网站路径问题
  • 阿克苏网站建设公司上海wordpress开发
  • 站长基地成都双流 网站建设
  • dota2海涛做的网站网站做百度推广划算吗
  • 常州网站关键词优化咨询百度怎样建设网站
  • 深圳市南山网站建设商标注册网上查询网
  • 传媒网站如何设计做网站的费用计入销售费用吗
  • 工信部个人备案网站可信吗专业图书商城网站建设