摘要:分為每次從里邊循環(huán)所有數(shù),已有值減去所有數(shù),新值作為已有值,繼續(xù)處理。遇到返回保存,負(fù)數(shù)去掉
"""
39. Combination Sum
Description
HintsSubmissionsDiscussSolution
Given a set of candidate numbers (C) (without duplicates) and a target number (T),
find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
""" import copy class Solution: def recursive(self,candidates,target_cur,nums_in,numlist_cur): """ 分為 每次從set里邊循環(huán)所有數(shù),已有值減去所有數(shù),新值作為已有值,繼續(xù)處理。遇到0 返回保存,負(fù)數(shù)去掉 :param candidates: :param target: :return: """ # numlist_cur_in=numlist_cur outlist=[] for index,num in enumerate(candidates): target_cur_new=target_cur-num if target_cur_new==0: numlist_cur.append(nums_in+[num]) elif target_cur_new>0: self.recursive(candidates[index:],target_cur_new,nums_in+[num],numlist_cur) else: pass def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ numlist_cur=[] out=self.recursive(candidates,target,[],numlist_cur) return numlist_cur # print(out) # print(numlist_cur) class Solution_: def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ res = [] self.rec(candidates, target, [], res) return res def rec(self, candidates, target, path, res): if target < 0: return if target == 0: res.append(path) return for i in range(len(candidates)): self.rec(candidates[i:], target - candidates[i], path + [candidates[i]], res) if __name__=="__main__": st=Solution() nums=[2, 3, 6, 7] target=6 out=st.combinationSum(nums,target) print(out)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/41423.html
摘要:參考思路和非常類似,只是這里需要增加進(jìn)行重復(fù)處理的部分。題目要求題目中新添的要求包括數(shù)組中存在重復(fù)值,而且數(shù)組中每個(gè)值只可以使用一次。需要注意的是,既然數(shù)組中存在重復(fù)的值,就要注意可能會(huì)將重復(fù)的情況加入結(jié)果數(shù)組。 參考 思路和leetcode39 combination sum 非常類似,只是這里需要增加進(jìn)行重復(fù)處理的部分。請參考我對leetcode39進(jìn)行解答的這篇博客。 題目要求 ...
摘要:在這道題中,我結(jié)合了遞歸的思想來。就是將當(dāng)前的值作為一個(gè)潛在的結(jié)果值加入一個(gè)結(jié)果數(shù)組將數(shù)組作為當(dāng)前結(jié)果傳入下一輪遞歸。 題目要求 Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the ca...
摘要:解題思路利用遞歸,對于每個(gè)根節(jié)點(diǎn),只要左子樹和右子樹中有一個(gè)滿足,就返回每次訪問一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...
摘要:只要我們能夠有一個(gè)以某一中間路徑和為的哈希表,就可以隨時(shí)判斷某一節(jié)點(diǎn)能否和之前路徑相加成為目標(biāo)值。 最新更新請見:https://yanjia.me/zh/2019/01/... Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addin...
摘要:動(dòng)態(tài)規(guī)劃法用表示最大子數(shù)組的結(jié)束下標(biāo)為的情形,則對于,有這樣就有了一個(gè)子結(jié)構(gòu),對于初始情形,遍歷就能得到這個(gè)數(shù)組,其最大者即可最大子數(shù)組的和。動(dòng)態(tài)規(guī)劃法想法巧妙,運(yùn)行效率也高,但是沒有普遍的適用性。 問題簡介 ??本文將介紹計(jì)算機(jī)算法中的經(jīng)典問題——最大子數(shù)組問題(maximum subarray problem)。所謂的最大子數(shù)組問題,指的是:給定一個(gè)數(shù)組A,尋找A的和最大的非空連續(xù)...
閱讀 1588·2021-09-24 10:38
閱讀 1524·2021-09-22 15:15
閱讀 3074·2021-09-09 09:33
閱讀 913·2019-08-30 11:08
閱讀 650·2019-08-30 10:52
閱讀 1263·2019-08-30 10:52
閱讀 2358·2019-08-28 18:01
閱讀 533·2019-08-28 17:55