更新 0046.全排列 python3版本

更新 0046.全排列 python3版本,比之前那个更简洁一点,少了个used数组
This commit is contained in:
jojoo15 2021-05-28 11:26:43 +02:00 committed by GitHub
parent fa25fab461
commit 14221a52fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -182,7 +182,23 @@ class Solution {
```
Python
```python3
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = [] #存放符合条件结果的集合
path = [] #用来存放符合条件的结果
def backtrack(nums):
if len(path) == len(nums):
return res.append(path[:]) #此时说明找到了一组
for i in range(0,len(nums)):
if nums[i] in path: #path里已经收录的元素,直接跳过
continue
path.append(nums[i])
backtrack(nums) #递归
path.pop() #回溯
backtrack(nums)
return res
```
Go
```Go