LeetCode 46-50 记录

46

https://leetcode-cn.com/problems/permutations/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from typing import List


class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def permutation(nums):
if len(nums) == 1:
yield nums[0:1]
else:
for perm in permutation(nums[1:]):
for i in range(len(perm) + 1):
yield perm[:i] + nums[0:1] + perm[i:]
return list(permutation(nums))


if __name__ == '__main__':
s = Solution()
print(s.permute([1, 2, 3]))

就是递归。想到了就是想到了。思路的话都好想,就是怎么转化成代码。


版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!