GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/077_Combinations.py at master · scrz2015/leetcode

Expand file tree

/

077_Combinations.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
43 lines (38 loc) · 1.22 KB
/

077_Combinations.py

Copy path

File metadata and controls

43 lines (38 loc) · 1.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
class Solution(object):
# def combine(self, n, k):
# """
# :type n: int
# :type k: int
# :rtype: List[List[int]]
# """
# res = []
# candidates = range(1, n + 1)
# self.get_combine(res, candidates, [], k, 0)
# return res
#
# def get_combine(self, res, candidates, prefix, k, start):
# # recursive
# if k == 0:
# res.append(prefix)
# for index in range(start, len(candidates)):
# self.get_combine(res, candidates,
# prefix + [candidates[index]],
# k - 1, index + 1)
def combine(self, n, k):
res = []
self.get_combine(res, [], n, k, 1)
return res
def get_combine(self, res, prefix, n, k, start):
# recursive with only one array
if k == 0:
res.append(list(prefix))
elif start <= n:
prefix.append(start)
self.get_combine(res, prefix,
n, k - 1, start + 1)
prefix.pop()
self.get_combine(res, prefix,
n, k, start + 1)
if __name__ == "__main__":
s = Solution()
print s.combine(4, 2)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.