leetcode_组合和-创新互联
typing import List
class Solution:
def combinationSum1(self, candidates: List[int], target: int) -> List[List[int]]:
#必须要排序 最后的结果可能会有顺序不一样但是元素一样的组合
candidates.sort()
res = []
reslist = []
def dfs(candidates,target,reslist):
if target == 0 and reslist not in res:
res.append(reslist)
return
if target<0:
return
for i in range(len(candidates)):
print(reslist)
#这个递归太难,再理解
dfs(candidates[i+1:],target-candidates[i],reslist+[candidates[i]])
dfs(candidates,target,reslist)
function(){ //外汇点差 http://www.kaifx.cn/mt4/kaifx/1749.html
return res
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
# for i in range(candidates):
res = []
reslist = []
def dfs(candidates,begin,target,reslist):
# for i in range(len(candidates)):
if target == 0 and reslist not in res:
res.append(reslist)
return
elif target<0 or begin>=len(candidates):
return
else:
# print(reslist,begin)
dfs(candidates,begin+1,target,reslist)
dfs(candidates,begin+1,target-candidates[begin],reslist+[candidates[begin]])
dfs(candidates,0,target,reslist)
return res
if name == "main":
a = Solution()
print(a.combinationSum1([2,3,6,7],7))
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:leetcode_组合和-创新互联
分享路径:http://azwzsj.com/article/codhdj.html