状态很不好
https://leetcode-cn.com/problems/permutations/
一道很有意思的题目
全排列 深度优先,说了很久,其实理解的不太好,递归也是
先写了代码
刷下题
class Solution:
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.ans = []
sub = []
self.dfs(nums,sub)
return self.ans
def dfs(self,nums,sub):
if len(nums) == len(sub):
self.ans.append(sub[:])
for m in nums:
if m in sub:
continue
sub.append(m)
self.dfs(nums,sub)
sub.remove(m)
网友评论