Skip to content

Commit 14221a5

Browse files
authored
更新 0046.全排列 python3版本
更新 0046.全排列 python3版本,比之前那个更简洁一点,少了个used数组
1 parent fa25fab commit 14221a5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

problems/0046.全排列.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,23 @@ class Solution {
182182
```
183183

184184
Python:
185-
185+
```python3
186+
class Solution:
187+
def permute(self, nums: List[int]) -> List[List[int]]:
188+
res = [] #存放符合条件结果的集合
189+
path = [] #用来存放符合条件的结果
190+
def backtrack(nums):
191+
if len(path) == len(nums):
192+
return res.append(path[:]) #此时说明找到了一组
193+
for i in range(0,len(nums)):
194+
if nums[i] in path: #path里已经收录的元素,直接跳过
195+
continue
196+
path.append(nums[i])
197+
backtrack(nums) #递归
198+
path.pop() #回溯
199+
backtrack(nums)
200+
return res
201+
```
186202

187203
Go:
188204
```Go

0 commit comments

Comments
 (0)