给出一组数字,返回该组数字的所有排列
例如:
[1,2,3]的所有排列如下
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], [3,2,1].
(以数字在数组中的位置靠前为优先级,按字典序排列输出。)
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], [3,2,1].
(以数字在数组中的位置靠前为优先级,按字典序排列输出。)
数据范围:数字个数
要求:空间复杂度 ,时间复杂度
[1,2,3]
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
[1]
[[1]]
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param num int整型一维数组 # @return int整型二维数组 # class Solution: def permute(self , num: List[int]) -> List[List[int]]: numlength = len(num) if numlength == 1: return [num] lens = 1 for i in range(1, numlength+1): lens = lens * i ret = [] for i in range(numlength): res = list(num) cur = res.pop(i) res = self.permute(res) for j in range(len(res)): mid = [cur] mid.extend(res[j]) ret.append(mid) return ret
class Solution: def permute(self , num: List[int]) -> List[List[int]]: n = len(num) res_all = [] # all result res = [] # result def dfs(res): # dfs函数是permute函数的闭包函数 if len(res) == n: res_all.append(res[:]) # res[:]可以开辟新内存存储结果,防止被下面的res.pop()影响 for i in range(n): if num[i] not in res: res.append(num[i]) dfs(res) res.pop() dfs(res) return res_all
class Solution: def permute(self , num: List[int]) -> List[List[int]]: if len(num)==1: return [[num[0]]] res=[] for i in range(len(num)): temp=num.copy() temp.pop(i) for item in self.permute(temp): res.append([num[i]]+item) return res
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param num int整型一维数组 # @return int整型二维数组 # class Solution: res=[] def trace(self,path, nums): if len(path)==len(nums): self.res.append(path[:])#为什么path不行 return for num in nums: if num in path: continue path.append(num) self.trace(path, nums) #回溯 path.pop() def permute(self , num: List[int]) -> List[List[int]]: # write code here num.sort() self.trace([],num) return self.res
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param num int整型一维数组 # @return int整型二维数组 # class Solution: def permute(self , num: List[int]) -> List[List[int]]: # write code here res = [] for p in self.permIter(num): res.append(list(p)) return res def permIter(self, num: List[int]): # never modify num # return copies if len(num) < 2: yield list(num) return others = num[1:] cached = list(others) for p in self.permIter(others): assert(others == cached) p.append(num[0]) yield list(p) for i in range(len(p)-1, 0, -1): p[i], p[i-1] = p[i-1], p[i] yield list(p)
class Solution: def permute(self , num: List[int]) -> List[List[int]]: # write code here n = len(num) if n == 1: return [num] res = [] for i in range(n): except_i = self.permute(num[:i]+num[i+1:]) for l in except_i: res.append([num[i]]+l) return res
class Solution: def permute(self , num: List[int]) -> List[List[int]]: # write code here def dfs(cur, nums): if len(cur) == len(num): return cur for index, i in enumerate(nums): tmp = dfs(cur + [i], nums[ : index] + nums[index + 1 : ]) if tmp: res.append(tmp) return # for循环结束, 返回None res = [] num.sort() dfs([], num) return res
class Solution: def permute(self , num: List[int]) -> List[List[int]]: # write code here result=[] def trace(path,choices): if len(path)==len(num): result.append(list(path)) return for item in choices: if item in path: continue path.append(item) trace(path, choices) path.pop() trace([], num) return result
class Solution: def permute(self , num: List[int]) -> List[List[int]]: # write code here res=[] def backtrace(node,path): if len(path)==len(num): res.append(path) return for i in range(len(node)): cur_node=node[:i]+node[i+1:] cur_path=path+[node[i]] backtrace(cur_node, cur_path) backtrace(num, []) return res
class Solution: def permute(self , num ): # write code here if num == []: return [] visited = [False] * len(num) res, path = [], [] def dfs(visited, path, num): if len(path) == len(num): res.append(path[:]) return for i in range(len(num)): if visited[i] == False: visited[i] = True path.append(num[i]) dfs(visited, path, num) path.pop() visited[i] = False for i in range(len(num)): visited[i] = True path.append(num[i]) dfs(visited, path, num) path.pop() visited[i] = False return sorted(res)
class Solution: def permute(self , num ): # write code here num.sort() def dfs(res,path,depth,used): if depth == n: res.append(path[:]) for i in range(n): if used[i] == 1: continue used[i] = 1 path.append(num[i]) dfs(res,path,depth+1,used) path.pop() used[i] = 0 res = [] n = len(num) used = [0]*n dfs(res,[],0,used) return res