LeetCode 39. 组合和 - 如何避免重复
我正在做 leetcode 39. 组合总和。:
给定一个不同整数
候选者
数组和一个目标
整数目标,返回所有唯一组合的列表候选人
所选数字总和为目标
。您可以按任何顺序返回组合。可以从
候选人
中选择相同号码,无限次。如果至少一个所选数字的频率不同,则两个组合是唯一的。对于给定的输入,保证总和为
目标
的唯一组合数量少于150
个组合。约束
1 <=候选人.length <= 30
1 <= 候选人[i] <= 200
候选
的所有元素都不同。1 <= 目标 <= 500
我能够编写以下代码:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
output = []
def backtrack(currNbr, comb):
if currNbr == 0:
output.append(comb.copy())
return
elif currNbr < 0:
return
for nbr in candidates:
comb.append(nbr)
currNbr = currNbr - nbr
backtrack(currNbr, comb)
comb.pop()
currNbr = currNbr + nbr
backtrack(target, comb = [])
return output
但是,我无法对其进行排序以避免重复。我在网上看到了一些解决方案,但它们似乎都使用索引i
。谁能向我解释一下如何更改我的代码以避免重复?
I am doing leetcode 39. Combination Sum.:
Given an array of distinct integers
candidates
and atarget
integer target, return a list of all unique combinations ofcandidates
where the chosen numbers sum totarget
. You may return the combinations in any order.The same number may be chosen from
candidates
an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.It is guaranteed that the number of unique combinations that sum up to
target
is less than150
combinations for the given input.Constraints
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
- All elements of
candidates
are distinct.1 <= target <= 500
I was able to write the following code:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
output = []
def backtrack(currNbr, comb):
if currNbr == 0:
output.append(comb.copy())
return
elif currNbr < 0:
return
for nbr in candidates:
comb.append(nbr)
currNbr = currNbr - nbr
backtrack(currNbr, comb)
comb.pop()
currNbr = currNbr + nbr
backtrack(target, comb = [])
return output
However, I cannot make it in sort to avoid duplicates. I have seen some solutions online but they all seem to use an index i
. Can anyone please explain to me, how to change my code to avoid duplicates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用索引
i
是很常见的。这是因为,一旦您迭代到组合
的下一个nbr
,在更深的递归中,不应该从combinations
中选择一个条目< em>在此处选择的之前。您可以在没有
i
的情况下执行此操作,而是传递combinations
的缩短副本,其中仅包含仍允许选择的条目。有几种方法可以做到这一点。当您还想缩短列表时,应该注意正确地迭代列表。此外,
pop()
比pop(0)
更高效。所以我选择使用reversed
以相反的顺序迭代:Yes, it is common to make use of the index
i
. This is because once you iterate to the nextnbr
ofcombinations
, nowhere in the deeper recursion, should an entry be selected fromcombinations
that comes before the one selected here.You can do this without
i
, and instead pass a shortened copy ofcombinations
, which will only have those entries which are still allowed to be selected.There are several ways to do this. One should take care of correctly iterating over a list when you also want to make it shorter. Also,
pop()
is more efficient thanpop(0)
. So I have opted to usereversed
to iterate in reversed order: