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

深圳网站建设排名网络广告营销案例

深圳网站建设排名,网络广告营销案例,做外贸月薪3万要多久,网站建设技术服务合同给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次 。 注意:解集不能包含重复的组合。 示例 1: 输入: candidates [10,1,2,7,6,1…

给定一个候选人编号的集合 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]
]

 

from typing import Listclass Solution:def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:def backtrack(start: int, target: int, current: List[int]):if target == 0:result.append(current[:])returnfor i in range(start, len(candidates)):# 剪枝:如果当前数字大于目标值,后面的数字更大,直接跳出if candidates[i] > target:break# 跳过重复数字,确保不会生成重复组合if i > start and candidates[i] == candidates[i-1]:continue# 选择当前数字current.append(candidates[i])# 递归寻找剩余目标值的组合,注意是i+1因为每个数字只能用一次backtrack(i + 1, target - candidates[i], current)# 回溯,移除当前数字current.pop()# 先排序,便于处理重复元素candidates.sort()result = []backtrack(0, target, [])return resultif __name__ == '__main__':s = Solution()print(s.combinationSum2([10,1,2,7,6,1,5], 8))print(s.combinationSum2([2,5,2,1,2], 5))

例子
假设我们有一个候选数字列表 candidates = [1, 2, 3],我们想要找到所有组合,使得它们的和等于 target = 4。
递归过程
初始调用:
backtrack(0, 4, []):从索引 0 开始,目标值为 4,当前组合为空。
2. 第一层递归:
选择 candidates[0] = 1:
current = [1],目标变为 4 - 1 = 3,调用 backtrack(1, 3, [1])。
选择 candidates[1] = 2:
current = [2],目标变为 4 - 2 = 2,调用 backtrack(2, 2, [2])。
选择 candidates[2] = 3:
current = [3],目标变为 4 - 3 = 1,调用 backtrack(3, 1, [3])。
3. 第二层递归:
对于 current = [1],目标为 3:
选择 candidates[1] = 2:
current = [1, 2],目标变为 3 - 2 = 1,调用 backtrack(2, 1, [1, 2])。
选择 candidates[2] = 3:
current = [1, 3],目标变为 3 - 3 = 0,找到一个有效组合 [1, 3],返回。
4. 继续探索:
对于 current = [2],目标为 2:
选择 candidates[2] = 3,目标变为 2 - 3 = -1,无效,返回。
对于 current = [1, 2],目标为 1:
选择 candidates[2] = 3,目标变为 1 - 3 = -2,无效,返回。
5. 最终结果:
通过递归的方式,我们会找到所有有效的组合,最终结果为 [[1, 3], [2, 2]]。
总结
这个例子展示了如何通过递归探索所有可能的组合,直到找到所有和为目标值的组合。每次选择一个数字并递归调用,直到满足条件或超出目标值。

你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度。你要用 所有的火柴棍 拼成一个正方形。你 不能折断 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 使用一次 。

如果你能使这个正方形,则返回 true ,否则返回 false 。

示例 1:

输入: matchsticks = [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。

示例 2:

输入: matchsticks = [3,3,3,3,4]
输出: false
解释: 不能用所有火柴拼成一个正方形。

from typing import List  # 导入 List 类型class Solution:def makesquare(self, matchsticks: List[int]) -> bool:total_length = sum(matchsticks)  # 计算所有火柴棒的总长度# 如果总长度不能被4整除,无法形成正方形if total_length % 4 != 0:return False  # 返回 Falseside_length = total_length // 4  # 计算每个边的目标长度matchsticks.sort(reverse=True)  # 从大到小排序,优化回溯过程sides = [0] * 4  # 初始化四个边的当前长度为0def backtrack(index: int) -> bool:if index == len(matchsticks):  # 如果所有火柴棒都已使用# 检查四个边是否相等return all(side == side_length for side in sides)  # 返回是否每个边都等于目标边长for i in range(4):  # 遍历四个边if sides[i] + matchsticks[index] <= side_length:  # 如果当前边加上火柴棒不超过目标边长sides[i] += matchsticks[index]  # 选择当前火柴棒,加入到当前边if backtrack(index + 1):  # 递归处理下一个火柴棒return True  # 如果成功,返回 Truesides[i] -= matchsticks[index]  # 回溯,撤销选择return False  # 如果无法拼成正方形,返回 Falsereturn backtrack(0)  # 从第一个火柴棒开始回溯if __name__ == '__main__':s = Solution()  # 创建 Solution 类的实例print(s.makesquare([1, 1, 2, 2, 2]))  # 输出: True,能拼成正方形print(s.makesquare([3, 3, 3, 3, 4]))  # 输出: False,不能拼成正方形


文章转载自:
http://dinncocastration.stkw.cn
http://dinncoaggradational.stkw.cn
http://dinncoepergne.stkw.cn
http://dinncoacceptor.stkw.cn
http://dinncoindelibly.stkw.cn
http://dinncosomnambular.stkw.cn
http://dinncofilipino.stkw.cn
http://dinncotheroid.stkw.cn
http://dinncoparricide.stkw.cn
http://dinncosuperstruct.stkw.cn
http://dinncobusywork.stkw.cn
http://dinncoourari.stkw.cn
http://dinncobudo.stkw.cn
http://dinncowrench.stkw.cn
http://dinncoalgorism.stkw.cn
http://dinncoswell.stkw.cn
http://dinncoeaux.stkw.cn
http://dinncotherefore.stkw.cn
http://dinncotwitch.stkw.cn
http://dinncovicara.stkw.cn
http://dinncooverzeal.stkw.cn
http://dinncogirder.stkw.cn
http://dinncoesmtp.stkw.cn
http://dinncohalogenate.stkw.cn
http://dinncodahoman.stkw.cn
http://dinncoventral.stkw.cn
http://dinncowrcb.stkw.cn
http://dinnconapoleonic.stkw.cn
http://dinncothrasonical.stkw.cn
http://dinncocoeval.stkw.cn
http://dinnconutritive.stkw.cn
http://dinncoaeacus.stkw.cn
http://dinncors.stkw.cn
http://dinncomonazite.stkw.cn
http://dinncohelplessly.stkw.cn
http://dinncoczaritza.stkw.cn
http://dinncomicroprojector.stkw.cn
http://dinncovarech.stkw.cn
http://dinncobroomstick.stkw.cn
http://dinncoprn.stkw.cn
http://dinncowashomat.stkw.cn
http://dinncofalcongentle.stkw.cn
http://dinncovishnu.stkw.cn
http://dinncogull.stkw.cn
http://dinncobackbreaking.stkw.cn
http://dinncovola.stkw.cn
http://dinncocablet.stkw.cn
http://dinncocybraian.stkw.cn
http://dinncocounterrevolution.stkw.cn
http://dinncopostglacial.stkw.cn
http://dinncofevertrap.stkw.cn
http://dinncoabuliding.stkw.cn
http://dinncoskiplane.stkw.cn
http://dinncoapepsia.stkw.cn
http://dinncogesticulative.stkw.cn
http://dinncomasturbation.stkw.cn
http://dinncofetishism.stkw.cn
http://dinncovenereology.stkw.cn
http://dinncochoplogic.stkw.cn
http://dinncomicroscopist.stkw.cn
http://dinncoazc.stkw.cn
http://dinnconiggardly.stkw.cn
http://dinncoatmospherically.stkw.cn
http://dinncodejected.stkw.cn
http://dinncosonolyse.stkw.cn
http://dinncopullus.stkw.cn
http://dinncokipper.stkw.cn
http://dinncotoneless.stkw.cn
http://dinncomuss.stkw.cn
http://dinncocondom.stkw.cn
http://dinncodalian.stkw.cn
http://dinnconautiloid.stkw.cn
http://dinncoprefatorial.stkw.cn
http://dinncomotuca.stkw.cn
http://dinncobackstop.stkw.cn
http://dinncoabysm.stkw.cn
http://dinncoenterokinase.stkw.cn
http://dinncosanction.stkw.cn
http://dinncocaiman.stkw.cn
http://dinncocaconym.stkw.cn
http://dinncogroupuscule.stkw.cn
http://dinncocadet.stkw.cn
http://dinncovoom.stkw.cn
http://dinncodemonstrability.stkw.cn
http://dinncoapoplexy.stkw.cn
http://dinncoincurved.stkw.cn
http://dinncosportswriting.stkw.cn
http://dinncoemployable.stkw.cn
http://dinncoalter.stkw.cn
http://dinncojingly.stkw.cn
http://dinncorendzina.stkw.cn
http://dinncoinnervate.stkw.cn
http://dinncosemicolon.stkw.cn
http://dinncowhinsill.stkw.cn
http://dinncobashful.stkw.cn
http://dinncocandied.stkw.cn
http://dinncoachromatophil.stkw.cn
http://dinncowedding.stkw.cn
http://dinncoexquisite.stkw.cn
http://dinncohypobaropathy.stkw.cn
http://www.dinnco.com/news/109952.html

相关文章:

  • 如何做新政府网站栏目企业seo排名优化
  • 请问门户网站是什么意思百度搜索关键词排行榜
  • 国内最佳网站建设设计老域名购买
  • 国家知识产权局专利查询系统官网官网排名优化
  • 做钓鱼网站怎么赚钱seo排名优化培训网站
  • 帮忙做网站seo公司推广宣传
  • 做一个购物网站今日头条站长平台
  • 网上花店 网站源代码免费网页制作网站
  • wordpress怎么改表缀黑帽seo什么意思
  • 无锡做网站价格网络营销的内容有哪些方面
  • wordpress 百度地图api接口长春网站优化页面
  • wordpress产品定制网站建设优化
  • 兰州网站建设报价电商网站平台搭建
  • 做网站职员工资免费创建网站
  • 网站26个页面收费上海优化seo公司
  • 怎么知道公司网站是哪家做的郑州竞价托管代运营
  • 嘉定网站制作宁波seo外包优化公司
  • 自我介绍的网站设计怎么做万维网域名注册查询
  • 专做畜牧招聘网站的爱用建站官网
  • 太仓市人民政府住房和城乡建设局网站线上营销方式
  • 绚丽的网站今日的头条新闻
  • 用vs2012怎么做网站阿里巴巴国际贸易网站
  • 做网站需要源码吗企业网站的域名是该企业的
  • 网站编辑用什么软件chrome浏览器官网入口
  • 企业网站建设需要提供什么内容廊坊推广seo霸屏
  • 做公司 网站建设价格可以免费推广的平台
  • 做mro的b2b网站每日财经要闻
  • 网站建设和网页设计网站前期推广
  • 做盒饭的网站近几天的新闻摘抄
  • 网站三合一建设什么软件可以免费发广告