-
Notifications
You must be signed in to change notification settings - Fork 305
/
77.py
42 lines (32 loc) · 926 Bytes
/
77.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Yu Zhou
# Youtube: https://youtu.be/imLl2s9Ujis
# 47. Combinations
# 这题超时了,但思路没错。。。。
# 搞不定。。
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
nums = []
for i in xrange(1, n+1):
nums.append(i)
self.res = []
self.used = [False] * len(nums)
def dfs(nums, temp, index):
if len(temp) == k:
self.res.append(temp[:])
return
for i in range(index, len(nums)):
if self.used[i]: continue
self.used[i] = True
temp.append(nums[i])
dfs(nums, temp, i)
temp.pop()
self.used[i] = False
dfs(nums, [], 0)
return self.res