您的位置:首页 > 财经 > 产业 > 环球设计_大学生创新创业大赛负责人简介_生意参谋官网_咖啡seo是什么意思

环球设计_大学生创新创业大赛负责人简介_生意参谋官网_咖啡seo是什么意思

2024/10/6 0:27:12 来源:https://blog.csdn.net/rgz147123/article/details/142717425  浏览:    关键词:环球设计_大学生创新创业大赛负责人简介_生意参谋官网_咖啡seo是什么意思
环球设计_大学生创新创业大赛负责人简介_生意参谋官网_咖啡seo是什么意思

参考文献 代码随想录

一、组合总和

给你一个 无重复元素 的整数数组 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
class Solution(object):def  __init__(self):self.result = []def combinationSum(self, candidates, target):""":type candidates: List[int]:type target: int:rtype: List[List[int]]""" # 2个优化,第一个就是通过拿目标值 减遍历的值,如果等于零,那么就收集结果,小于零则结束# 第二个,就对数组排序,如果当前的元素值大于target那么 直接结束candidates.sort()self.backtracking(candidates,  target, [] , 0)return self.resultdef backtracking(self, li, target, t ,startIndex):if target == 0:   # 终止条件   这里的终止 条件就是当目标值等于0的时候,就 收集结果self.result.append(t[:])returnif target < 0:returnfor i in range(startIndex, len(li)):if  li[i] >  target:breakself.backtracking(li, target - li[i], t + [li[i]], i)

二 、组合总和 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(object):def __init__(self):self.result = []def combinationSum2(self, candidates, target):""":type candidates: List[int]:type target: int:rtype: List[List[int]]"""candidates.sort()   # 排序 为了让 相同的 元素相邻 ,如果 前一个元素已经取了,那么后面 相同 的元素就不用取了,因为以第一个 元素开头的元素组合已经取到了,那么后面开头与 前一个 开头的元素相同的组合已经 被取到了,所以 后面的开头 相同的就不用取了print(candidates)self.backtracking(candidates, target, [], 0)return  self.resultdef backtracking(self, candidates, target,  li, startIndex):  # startIndex防止同一个 元素 使用多次if target == 0:self.result.append(li[:])returnif target < 0:returnfor i in range(startIndex, len(candidates)):if candidates[i] >  target:breakif i > startIndex and candidates[i] == candidates[i - 1]:  # 为什么是i>startIndex呢?因为每次要判断取出的 元素的开头 是否与之前的相同,如果相同,那么则继续continueself.backtracking(candidates, target -  candidates[i], li + [candidates[i]], i + 1)

三、分割回文串

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 

回文串

 。返回 s 所有可能的分割方案。

示例 1:

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

示例 2:

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

提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成

问题分析

        在字符串分割问题中,你通常需要检查从某个起始索引到当前结束索引的子串,而不是从某个起始索引到整个字符串的末尾。使用 self.isPromist(s, i, len(s) - 1) 会导致以下问题:

  1. 范围不准确:你实际检查的是从 i 到字符串末尾的部分,而不是从 startIndex 到当前结束索引 i 的部分。这可能会返回错误的结果,因为你并不关心后面的部分。

  2. 逻辑不符:在分割字符串并检查每个子串时,通常需要判断当前子串(即从 startIndexi 的子串)是否为回文,而不是从某个索引 i 到末尾。

class Solution(object):def __init__(self):self.result = []def partition(self, s):""":type s: str:rtype: List[List[str]]"""self.backtracking(s, [], 0)return self.resultdef backtracking(self, s, tmp, startIndex):if len(s) == startIndex:  # 这代表的是已经切割到末尾了,那么就要收集结果 self.result.append(tmp)return for i in range(startIndex, len(s)):if self.isPromist(s, startIndex, i):  #  这里为什么转startIndex和i呢,因为stratIndex是切割线 ,而 i一直在移动 ,每次移动都是一个子串,如果你转的是i和长度的话检查从索引 i 到字符串 s 的最后一个字符是否是回文。在字符串分割问题中,你通常需要检查从某个起始索引到当前结束索引的子串,而不是从某个起始索引到整个字符串的末尾。使用 self.isPromist(s, i, len(s) - 1) 会导致以下问题:# 范围不准确:你实际检查的是从 i 到字符串末尾的部分,而不是从 startIndex 到当前结束索引 i 的部分。这可能会返回错误的结果,因为你并不关心后面的部分。# 逻辑不符:在分割字符串并检查每个子串时,通常需要判断当前子串(即从 startIndex 到 i 的子串)是否为回文,而不是从某个索引 i 到末尾。self.backtracking( s, tmp  +  [s[startIndex : i + 1]], i + 1)def isPromist(self, s, start, end):while start  <= end:if s[start] != s[end]:return Falsestart += 1end -= 1return True

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com