力扣 46. 全排列

递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def permute(nums):
def add(ret, num):
tmp = []
for r in ret:
for i in range(len(r) + 1):
tmp.append(r[:i] + [num] + r[i:])
return tmp

if not nums:
return []
ret = [[nums[0]]]
for i in range(1, len(nums)):
ret = add(ret, nums[i])

return ret
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信