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

濮阳住房和城乡建设部网站网络营销发展方案策划书

濮阳住房和城乡建设部网站,网络营销发展方案策划书,.net core 做网站,全网最大的精品网站每天一题,防止痴呆题目示例分析思路1题解1分析思路2题解2👉️ 力扣原文 题目 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形…

每天一题,防止痴呆

  • 题目
  • 示例
  • 分析思路1
  • 题解1
  • 分析思路2
  • 题解2

👉️ 力扣原文

题目

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

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

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

示例

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

分析思路1

使用回溯算法:
使用了一个 start 变量来避免重复搜索。每次迭代从 start 开始,而不是从0开始。这是因为我们不需要在同一层次上搜索相同的元素,这会导致重复组合的出现。

此外,我们还进行了剪枝操作,即当候选数大于目标数时,我们停止搜索。这可以大大减少搜索空间,提高代码效率。

最后,我们使用了一个 result 列表来存储所有找到的组合。每当找到一个组合时,我们将其添加到结果列表中。

题解1

class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();List<Integer> temp = new ArrayList<>();Arrays.sort(candidates); // 排序,便于剪枝backtrack(candidates, target, 0, temp, result);return result;}private void backtrack(int[] candidates, int target, int start, List<Integer> temp, List<List<Integer>> result) {if (target < 0) {return;}if (target == 0) {result.add(new ArrayList<>(temp));return;}for (int i = start; i < candidates.length; i++) {if (candidates[i] > target) { // 剪枝break;}temp.add(candidates[i]);backtrack(candidates, target - candidates[i], i, temp, result); // 注意这里传入的参数temp.remove(temp.size() - 1);}}
}

执行结果
在这里插入图片描述

分析思路2

动态规划:
使用了一个二维数组 dp 来存储中间结果,其中 dp[i] 表示数字和为 i 时的所有组合。初始时,dp[0] 存储一个空列表,表示数字和为 0 时只有一种组合,即不选任何数字。
接下来,对于每个数字 candidates[i],从 candidates[i] 开始枚举数字和 j,如果 dp[j - candidates[i]] 不为空,则将 dp[j - candidates[i]] 中的每个组合都加上 candidates[i],得到一个新的组合,将其加入到 dp[j] 中。这样,当枚举到 target 时,dp[target] 中存储的就是所有符合要求的组合。
最后,如果 dp[target] 为空,则返回一个空列表,否则返回 dp[target]。

题解2

class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>>[] dp = new List[target + 1];dp[0] = new ArrayList<>();dp[0].add(new ArrayList<>());for (int i = 0; i < candidates.length; i++) {for (int j = candidates[i]; j <= target; j++) {if (dp[j - candidates[i]] != null) {if (dp[j] == null) {dp[j] = new ArrayList<>();}for (List<Integer> list : dp[j - candidates[i]]) {List<Integer> temp = new ArrayList<>(list);temp.add(candidates[i]);dp[j].add(temp);}}}}return dp[target] == null ? new ArrayList<>() : dp[target];}
}

执行结果
在这里插入图片描述

http://www.yidumall.com/news/62303.html

相关文章:

  • http网站防劫持怎么做谷歌关键词搜索量数据查询
  • wordpress可以做电影站东莞做网站优化
  • 网站搭建公司排行一键生成网页
  • 企业网站源码变现方法搭建一个网站平台需要多少钱
  • 网站备案找回西安百度推广联系方式
  • 手机支付网站开发公司建网站流程
  • 阿里网站建设App开发新闻最新消息
  • 河南省建设厅网站门户长沙百度快速优化
  • 网站首页怎么做代刷网站推广快速
  • 赤峰建筑人才网青岛谷歌优化公司
  • 怎么建设自己网站首页百度推广开户2400
  • 新疆住房和城乡建设网站班级优化大师怎么下载
  • 网站优化和提升网站排名怎么做如何优化搜索关键词
  • 做网站要有什么团队网站关键词优化排名技巧
  • 广西南宁网站建设公司专业seo网络推广
  • 用网站做的人工智能网站优化的方式有哪些
  • 淄博的大型网站建设交换友情链接的渠道
  • 推荐一下做图文的网站百度上看了不健康的内容犯法吗
  • wordpress 虚幻大学惠州seo代理
  • 鲅鱼圈做网站网工资页多少钱一个月seo快速建站
  • 大理公司网站建设爱站网seo工具
  • 国家信息企业信用公示网seo专业学校
  • php网站中水印怎么做网络舆情管理
  • 课程设计代做网站php发布新闻最快的网站
  • 怎样建立自己的网站卖东西竞价托管推广哪家好
  • 临淄网站建设公司网站seo优化发布高质量外链
  • 电销系统开发搜外seo视频 网络营销免费视频课程
  • 贵州省城乡与住房建设部网站广州优化营商环境条例
  • 网站建设的方案实施包括网络营销课程感悟
  • 汽车之家网站系统是什么做的seo知识总结