forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinations.py
36 lines (33 loc) · 818 Bytes
/
combinations.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
# Time: O(n!)
# Space: O(n)
#
# Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
#
# For example,
# If n = 4 and k = 2, a solution is:
#
# [
# [2,4],
# [3,4],
# [2,3],
# [1,2],
# [1,3],
# [1,4],
# ]
#
class Solution:
# @return a list of lists of integers
def combine(self, n, k):
result = []
self.combineRecu(n, result, 0, [], k)
return result
def combineRecu(self, n, result, start, intermediate, k):
if k == 0:
result.append(intermediate[:])
for i in xrange(start, n):
intermediate.append(i + 1)
self.combineRecu(n, result, i + 1, intermediate, k - 1)
intermediate.pop()
if __name__ == "__main__":
result = Solution().combine(4, 2)
print result