主要在于nexts的选择,
nexts是指拿走当前元素i后剩下的元素即为,0-i, i+1-最后,
因此下次递归nexts = nexts[:i] + nexts[i+1:]
class Solution:
def permute(self , num: List[int]) -> List[List[int]]:
rs = []
def go(nexts, temp):
if len(nexts) == 0:
rs.append(temp)
for i in range(len(nexts)):
temp.append(nexts[i])
go(nexts[:i] + nexts[i+1:], temp[:])
temp = temp[:-1]
go(num, [])
return rs

京公网安备 11010502036488号