Fetch the repository succeeded.
# 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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。