77. 组合
思路
想象成由1-n组成的列表可以生成长度为k的组合数
n=4 nums=[1,2,3,4]
k=2:
[1,2] [1,3][1,4]…
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
res = []
def backtrack(nums,track,k):
if len(track)==k:
res.append(track[:])
return
for i in range(len(nums)):
track.append(nums[i])
backtrack(nums[i+1:],track,k)
track.pop()
num=[]
for i in range(1,n+1):
num.append(i)
backtrack(num,[],k)
return res
原文地址:https://blog.csdn.net/huanxianxianshi/article/details/142494222
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!