-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46_permutations.py
More file actions
49 lines (39 loc) · 1.25 KB
/
Copy path46_permutations.py
File metadata and controls
49 lines (39 loc) · 1.25 KB
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
44
45
46
47
48
49
from typing import List
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def dfs(idx):
if idx == len(nums):
ans.append(option[:])
return
for i, v in enumerate(visted):
if v == 0:
option[idx] = nums[i]
visted[i] = 1
dfs(idx + 1)
visted[i] = 0
ans = []
visted = [0] * len(nums)
option = [0] * len(nums)
dfs(0)
return ans
def permute2(self, nums: List[int]) -> List[List[int]]:
def dfs(idx):
if idx == len(nums):
ans.append(option[:])
return
for i in range(idx, len(nums)):
option[idx] = nums[i]
nums[i], nums[idx] = nums[idx], nums[i]
dfs(idx + 1)
nums[i], nums[idx] = nums[idx], nums[i]
ans = []
option = [0] * len(nums)
dfs(0)
return ans
def test():
ans = Solution().permute([1, 2, 3])
expect = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
assert ans == expect, ans
print(Solution().permute2([1, 2, 3]))
if __name__ == "__main__":
test()