Skip to content

Commit 00673fe

Browse files
authored
Create 46_Permutations.md
1 parent 08d13d9 commit 00673fe

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

46_Permutations.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 46. Permutations
2+
3+
Given a collection of distinct integers, return all possible permutations.
4+
5+
Example:
6+
7+
Input: [1,2,3]
8+
Output:
9+
[
10+
[1,2,3],
11+
[1,3,2],
12+
[2,1,3],
13+
[2,3,1],
14+
[3,1,2],
15+
[3,2,1]
16+
]
17+
18+
```python
19+
def permute(self, nums: List[int]) -> List[List[int]]:
20+
result = []
21+
if len(nums) ==0: return result
22+
23+
def gen_pattern(build, index):
24+
if len(nums) == index:
25+
result.append(build)
26+
return
27+
28+
for num in nums:
29+
if not num in build:
30+
gen_pattern(build+[num], index+1)
31+
32+
gen_pattern([], 0)
33+
return result
34+
```
35+
36+
37+
```
38+
Runtime: 36 ms, faster than 86.09% of Python3 online submissions for Permutations.
39+
Memory Usage: 14.3 MB, less than 100.00% of Python3 online submissions for Permutations.
40+
```

0 commit comments

Comments
 (0)