File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments