Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit59a728e

Browse files
committed
Fixed
1 parent3a6ffe2 commit59a728e

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

‎combinations/solution.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
classSolution:
2-
# @return a list of lists of integers
1+
"""
2+
Given two integers n and k, return all possible combinations of k numbers out
3+
of 1 ... n.
4+
5+
For example,
6+
If n = 4 and k = 2, a solution is:
7+
8+
[
9+
[2,4],
10+
[3,4],
11+
[2,3],
12+
[1,2],
13+
[1,3],
14+
[1,4],
15+
]
16+
"""
17+
18+
19+
classSolution(object):
320
defcombine(self,n,k):
21+
"""
22+
:type n: int
23+
:type k: int
24+
:rtype: List[List[int]]
25+
"""
426
a=range(1,n+1)
527
returnself.combine_aux(a,k)
628

‎permutations/solution.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
classSolution:
2-
# @param num, a list of integer
3-
# @return a list of lists of integers
4-
defpermute(self,num):
5-
ifnotnum:
1+
"""
2+
Given a collection of numbers, return all possible permutations.
3+
4+
For example,
5+
[1,2,3] have the following permutations:
6+
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
7+
8+
"""
9+
10+
11+
classSolution(object):
12+
defpermute(self,nums):
13+
"""
14+
:type nums: List[int]
15+
:rtype: List[List[int]]
16+
"""
17+
ifnotnums:
618
return [[]]
719
else:
820
res= []
9-
fori,einenumerate(num):
10-
rest=num[:i]+num[i+1:]
21+
fori,einenumerate(nums):
22+
rest=nums[:i]+nums[i+1:]
1123
rest_perms=self.permute(rest)
1224
forperminrest_perms:
1325
perm.append(e)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp