2023.6.28
因为没有重复元素,因此可以不用单独列一个used集合去去重,只要self.nums[i] in self.path就可以直接剪去。
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
self.result = []
self.path = []
self.nums = nums
self.n = len(nums)
self.backtracking()
return self.result
def backtracking(self):
if len(self.path) == self.n:
self.result.append(self.path[:])
for i in range(self.n):
if self.nums[i] in self.path:
continue
self.path.append(self.nums[i])
self.backtracking()
self.path.pop()