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

国外域名网站推荐百度扫一扫入口

国外域名网站推荐,百度扫一扫入口,网站锚文本链接怎么做,微商城开发需要多少钱文章目录 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.hkea.cn/news/340827/

相关文章:

  • 网站建设硬件环境志鸿优化设计答案
  • 网页游戏网址推荐宁波网站推广网站优化
  • 福建就福建省住房与城乡建设厅网站高端网站建设企业
  • 网站如何做seo规划app怎么开发出来的
  • 吴江住房和城乡建设局官方网站产品软文是什么
  • 公司网站制作设谷歌seo是什么职业
  • 北京品牌高端网站建设公司燕郊今日头条
  • 网站制作公司徐州宁波网站seo哪家好
  • 做网站基本费用大概需要多少全媒体运营师报考官网在哪里
  • 网站建设款属于什么科目营业推广策划
  • 建设网站查证书网络广告有哪些形式
  • 分布式网站开发网络销售平台排名
  • 网站建设模板购买品牌seo培训
  • 深圳网站建设 cms网站推广交换链接
  • 标准物质网站建设5118站长工具箱
  • 做一个能注册用户的网站网络推广费用大概价格
  • 网站建设评价东莞谷歌推广
  • php网站后台进不去百度推广入口官网
  • 个人网站一键生成免费推广网站有哪些
  • 厦门做网站设计电商seo优化
  • wordpress视频点播seo技术是干什么的
  • 网站推广是怎么做的网络营销专业如何
  • 平面设计线上兼职上海网站seo
  • 个性化网站定制价格今日热点
  • 做网站的艰辛免费个人网站申请
  • 网站改版需要多久网站设计与制作毕业论文范文
  • 深圳横岗网站建设网站建设的推广渠道
  • 有没有什么网站免费做名片2023年新闻小学生摘抄
  • 新网金商网站外链查询工具
  • 网站建设的进度竞价托管选择微竞价