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

动画制作精灵seo销售是做什么的

动画制作精灵,seo销售是做什么的,找人做网站不算诈骗罪吗,wordpress转discuz一、39. 组合总和 题目链接:39. 组合总和 题目描述: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意…

一、39. 组合总和

题目链接:39. 组合总和
题目描述:

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

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

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

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40
算法分析:

利用经典的回溯算法。

首先创建一个二维数组用来存放所有组合的结果集,以及一个用来遍历每种合理组合的一维数组。

然后调用递归,纵向遍历组合,

传递参数:无重复数组,遍历数组的起始下标。

递归结束条件:当组合中的总和等于目标值时,将组合放入结果集,然后返回,如果组合中的总和大于目标值,则直接返回结束递归。

从起始下标横向遍历无重复数组,candidates[i]插入组合,总和sum加上candidates[i]的值,然后递归判断该组合是否满足,最后再回溯,将candidates[i]从组合中拿出来,sum减去candidates[i]的值,然后进行下一层for循环。

代码如下:

class Solution {List<List<Integer>>result = new ArrayList<>();//用来存放所有组合的结果集LinkedList<Integer> path = new LinkedList<>();//用来遍历每种组合的一维数组int T;//将目标整数定义在全局区域int sum;//记录组合总和int len;//记录数组candidates的长度public void backTravel(int[] candidates, int startIndex) {  if(sum == T) {//如果组合总和等于目标值,将改组和放入结果集,然后返回result.add(new LinkedList<Integer>(path));return;}else if(sum > T) return;//由于数组中每个元素(2 <= candidates[i] <= 40),所以当总和大于目标值时,后面无论加多少个元素,总和一定大于target,所以之间返回。for(int i = startIndex; i < len; i++) {//从起始下标遍横向历数组path.add(candidates[i]);sum += candidates[i];backTravel(candidates, i);//递归path.pollLast();//回溯sum -= candidates[i];}}public List<List<Integer>> combinationSum(int[] candidates, int target) {T = target;sum = 0;len = candidates.length;backTravel(candidates, 0);return result;}
}

二、40. 组合总和 II

题目链接:40. 组合总和 II
题目描述:

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

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

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

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30
算法分析:

这道题的做法跟上一道题类似,不过要注意的时要对于重复的组合进行去重操作。

具体去重操作为:

首先回溯之前对数组进行排序,如此相同的元素会放在一起。

然后再横向遍历数组是,对同一个元素重复出现在同一个位置去重(注意同一个元素出现在不同位置时不去重)。

代码如下:

class Solution {List<List<Integer>>result = new ArrayList<>();//存放所有组合的结果集LinkedList<Integer> path = new LinkedList<>();//搜索每种组合int T;int sum;int len;public void backTravel(int[] candidates, int startIndex) {if(sum == T) {//如果总和等于目标值,将组合放入结果集返回result.add(new LinkedList<>(path));return;}else if(sum > T) return;//如果总和大于目标值,直接返回for(int i = startIndex; i < len; i++) {//从起始下标横向遍历数组if(i > startIndex && candidates[i] == candidates[i - 1]) continue;//一个元素重复出现在同一位置时,进行去重path.add(candidates[i]);sum += candidates[i];backTravel(candidates, i + 1);//递归path.removeLast();//回溯sum -= candidates[i];}}public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);T = target;sum = 0;len = candidates.length;backTravel(candidates, 0);return result;}
}

三、131. 分割回文串

题目链接:131. 分割回文串
题目描述:

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

示例 2:

输入:s = "a"
输出:[["a"]]

提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成
算法分析:

做法跟上两道题类似,也是用回溯算法不过在对每种方案添加元素(字符串时),要判断一下该子串是否是回文串。

所以我们还要在上面的基础上增加一个判断子串是否是回文串的方法。

具体待码如下:

class Solution {List<List<String>> result = new ArrayList<>();//用来存放每种方案的结果集LinkedList<String> path = new LinkedList<>();//用来遍历每种方案int len;//字符串的长度public boolean isPartition(String s, int left, int right) {//判断字串是否是回文串while(left <= right) {if(s.charAt(left) != s.charAt(right)) return false;left++;right--;}return true;}public void backTravel(String s, int startIndex) {if(startIndex == len) {//如果起始下标等于字符串的长度,说明有一个分割方案了,将方案放入结果集,然后返回result.add(new LinkedList<>(path));return;}else if(startIndex > len) return;//如果起始下标大于字符串长度,说明没有分割方案,直接返回。for(int i = startIndex; i < len; i++) {//遍历从起始下标到结尾的子串,并判断从起始下标到i的字串是否是回文串if(isPartition(s, startIndex, i) != true) continue;//如果不是回文串,继续下一层for循环。path.add(s.substring(startIndex, i + 1));backTravel(s, i + 1);//递归path.removeLast(); //回溯}}public List<List<String>> partition(String s) {len = s.length();backTravel(s, 0);return result;}
}

总结

回溯时,我们不要只会对整数数组回溯,还要会对各种数组进行回溯。

http://www.ds6.com.cn/news/24270.html

相关文章:

  • 公司网站怎么做优化域名查询seo
  • wordpress专栏插件关键词优化seo优化排名
  • 网站上的3d怎么做的南宁网站seo外包
  • 政府网站建设的脚注企业网站建设费用
  • 做aa视频网站数字营销软件
  • 什么是seo是什么意思太原网站制作优化seo公司
  • 小程序订单管理系统seo根据什么具体优化
  • 专业服务网站建设北京网站优化策略
  • 网站大改版网站广告收费标准
  • ubuntu本地网站建设重庆百度推广排名
  • 网站建设有哪些名词sem账户托管外包
  • 深圳网站建设定制网站seo站长工具
  • 遵义网站开发东莞网站建设公司
  • 做平面还有什么素材网站营销的四种方式
  • 昆明做网站建设技巧公司会员营销
  • 手机网站建设合同qq群怎么优化排名靠前
  • 上海专业高端网站建设服务西安网络seo公司
  • 西安网站手机网站建设萌新seo
  • 个人wordpress 手机电脑优化大师
  • 网站开发的三层架构怎么自己做网站
  • 常州app网站百度快速收录软件
  • 大型网站制作重庆百度竞价开户
  • 公司网站开发交接注意事项seo品牌优化
  • 佛山做推广网站的百度搜索量
  • 微信saas平台广州seo成功案例
  • 韩国网站怎么打开免费友情链接平台
  • 做标书有哪些好网站网站网络排名优化方法
  • 博客网站 做淘宝客实时热点新闻事件
  • 网站建设流程 知乎郑州网络营销公司哪个好
  • 高端品牌logo图片seo优化有哪些