生活随笔
收集整理的這篇文章主要介紹了
leetcode--数组(Medium2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019.08.09
39.組合總數
def combinationSum(self
, candidates
: List
[int], target
: int) -> List
[List
[int]]:candidates
.sort
()n
= len(candidates
)res
= []def helper(i
, tmp_sum
, tmp
):if tmp_sum
> target
or i
== n
:return if tmp_sum
== target
:res
.append
(tmp
)return helper
(i
, tmp_sum
+ candidates
[i
],tmp
+ [candidates
[i
]])helper
(i
+1, tmp_sum
,tmp
)helper
(0, 0, [])return res
def combinationSum(self
, candidates
: List
[int], target
: int) -> List
[List
[int]]:candidates
.sort
()n
= len(candidates
)res
= []def backtrack(i
, tmp_sum
, tmp
):if tmp_sum
> target
or i
== n
:return if tmp_sum
== target
:res
.append
(tmp
)return for j
in range(i
, n
):if tmp_sum
+ candidates
[j
] > target
:breakbacktrack
(j
,tmp_sum
+ candidates
[j
],tmp
+[candidates
[j
]])backtrack
(0, 0, [])return res
總結
以上是生活随笔為你收集整理的leetcode--数组(Medium2)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。