From 14221a52fe02d489330b6fc254d187de99c99ae2 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Fri, 28 May 2021 11:26:43 +0200 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 0046.全排列 python3版本,比之前那个更简洁一点,少了个used数组 --- problems/0046.全排列.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 0beb45cf..3ead73e3 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -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