LeetCode 46_ 全排列

1. 题目描述

 

2. 代码

1 import itertools
2 class Solution:
3     def permute(self, nums: List[int]) -> List[List[int]]:
4         n = len(nums)
5         temp = itertools.permutations(nums)
6         result = []
7         for i in temp:
8             result.append(list(i))
9         return result

思路: 利用Python itertools内置库, permutations全排列, 比如输出123的全部情况.

1 from itertools import permutations
2 print(list(permutations('123')))
1 [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]

 

posted @ 2020-10-23 21:32  vv_869  阅读(73)  评论(0编辑  收藏  举报