1 Star 3 Fork 5

刘炜/python LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0040组合总和II.py 996 Bytes
一键复制 编辑 原始数据 按行查看 历史
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
rlt = []
path = []
right = len(candidates)
if right == 0:
return rlt
candidates.sort()
self.sumcal(candidates, 0, right, path, rlt, target, 0)
return rlt
def sumcal(self, candidates, begin, end, path, rlt, target, sumnow):
if sumnow == target:
rlt.append(path[:])
return
for index in range(begin, end):
if index != begin and candidates[index] == candidates[index-1]:
continue
sumnow = sumnow + candidates[index]
if sumnow > target:
break
elif sumnow < target and index == end:
break
path.append(candidates[index])
self.sumcal(candidates, index + 1, end, path, rlt, target, sumnow)
path.pop()
sumnow = sumnow - candidates[index]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/new_way/python-LeetCode.git
git@gitee.com:new_way/python-LeetCode.git
new_way
python-LeetCode
python LeetCode
master

搜索帮助