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

2018爱情动做网站seo营销推广公司

2018爱情动做网站,seo营销推广公司,石家庄公司做网站,网站的图片要会员才能下载怎么做原题链接🔗:组合总和 难度:中等⭐️⭐️ 题目 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 …

原题链接🔗:组合总和
难度:中等⭐️⭐️

题目

给你一个 无重复元素 的整数数组 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

题解

  1. 解题思路
  • LeetCode 上的 “组合总和”(Combination Sum)问题是一个典型的回溯算法问题。这个问题要求你从给定的候选数字数组中找出所有可能的组合,这些组合的元素之和等于给定的目标和。以下是解决这个问题的一种通用思路:

  • 问题描述

    • 给定一个无重复元素的数组 candidates 和一个目标数 target,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。
  • 解题思路

    • 回溯算法:使用回溯算法来尝试所有可能的组合。 排序:对 candidates 数组进行排序,这有助于剪枝,因为如果当前元素大于目标数,可以停止当前路径的搜索。
    • 递归函数:定义一个递归函数,该函数接收当前索引 start,当前组合 path,以及当前和 current_sum。
    • 终止条件:如果 current_sum 等于 target,则将当前组合添加到结果列表中。
    • 剪枝:如果 current_sum 超过 target 或者 start 超过数组长度,停止当前路径的搜索。
    • 循环遍历:从 start 开始遍历数组,对于每个元素,将其添加到当前组合中,并递归调用函数,然后回溯(即从当前组合中移除该元素)。
  1. c++ demo
#include <vector>
#include <algorithm>
#include <iostream>void findCombinations(std::vector<int>& candidates, int target, int start, std::vector<int>& current, std::vector<std::vector<int>>& result) {// 如果当前和等于目标,则将当前组合添加到结果中if (target == 0) {result.push_back(current);return;}for (int i = start; i < candidates.size(); ++i) {// 跳过相同的元素以避免重复的组合if (i > start && candidates[i] == candidates[i - 1]) continue;// 如果当前元素大于目标,则不需要继续搜索if (candidates[i] > target) break;// 将当前元素添加到组合中current.push_back(candidates[i]);// 递归调用,使用 i + 1 作为新的起始索引,因为我们可以重复使用相同的元素findCombinations(candidates, target - candidates[i], i, current, result);// 回溯,移除当前元素current.pop_back();}
}std::vector<std::vector<int>> combinationSum(std::vector<int>& candidates, int target) {// 对候选数组进行排序std::sort(candidates.begin(), candidates.end());std::vector<int> current;std::vector<std::vector<int>> result;// 从索引0开始,使用当前数组作为候选数组findCombinations(candidates, target, 0, current, result);return result;
}int main() {std::vector<int> candidates = { 2, 3, 6, 7 };int target = 7;std::vector<std::vector<int>> result = combinationSum(candidates, target);// 打印结果for (const auto& combination : result) {std::cout << "{";for (size_t i = 0; i < combination.size(); ++i) {std::cout << combination[i];if (i < combination.size() - 1) std::cout << ", ";}std::cout << "}" << std::endl;}return 0;
}
  • 输出结果:

{2, 2, 3}
{7}

  1. 代码仓库地址:combinationSum
http://www.tj-hxxt.cn/news/18718.html

相关文章:

  • 成都网站建设赢展南宁网站seo排名优化
  • 郑州建网站的公司国家认可的赚钱软件
  • 中企动力做的网站怎么样刺激广告
  • 做网站的一个黑点符号百度95099如何转人工
  • 毕业论文引用网站数据怎么做脚注广州百度竞价开户
  • 主备网站服务器自动切换 win2003百度 竞价排名
  • 政府网站建设安全管理办法seo快速排名源码
  • 旅游酒店网站建设产品推广软文500字
  • 做公司网站500元搜索引擎优化分析
  • 河南住房和城乡建设厅职称网站国家认可的赚钱软件
  • 学做网站根学ps有前途吗百姓网推广怎么收费标准
  • 合作做网站百度seo关键词排名 s
  • 接网站建设的单子免费人脉推广
  • 网站做好了前端 后端怎么做今日最新新闻
  • 适合个人网站成都专门做网络推广的公司
  • 小程序代运营怎么收费seo sem是啥
  • 怎么给一个花店做网站建设su搜索引擎优化
  • 国内医疗美容网站建设免费个人网站怎么建立
  • 做网站网页需要多久宣传软文怎么写
  • 模板网站可以做备案吗上海网络推广排名公司
  • 怎么提高网站流量长春网络优化最好的公司
  • 网站做联盟收入西安关键词快速排名
  • 网站做任务赚qb唯尚广告联盟平台
  • 怎么知道网站的空间是谁做的免费的网页网站
  • 公司做网站域名的好处宁波seo优化公司排名
  • 制作app需要哪些知识seo排名优化有哪些
  • HTML asp 网站百度指数关键词工具
  • 有了网站怎样做公众号百度客服中心
  • 怎么安装wordpress模板历下区百度seo
  • 网站诚信备案百度竞价推广流程