class Solution:
# @return a list of lists of integers
def combine(self, n, k):
rst=[]
crt=[0 for i in range(k)]
p=0
if 0<k<=n:
while p>=0:
crt[p]+=1
for i in range(p+1,k):
crt[i]=crt[i-1]+1
rst.append(crt[:])
p=k-1
while p>=0 and crt[p]==n-(k-1-p):
p-=1
return rst
Cheating code:
class Solution:
# @return a list of lists of integers
def combine(self, n, k):
return map(list,list(itertools.combinations(range(1,n+1),k)))
No comments :
Post a Comment