Source of the topic
https://leetcode.com/problems/permutations/
Given A collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
The following permutations:
[1,2,3]
,,,,, [1,3,2]
[2,1,3]
[2,3,1]
[3,1,2]
and [3,2,1]
.
Test instructions Analysis
INPUT:A List
Output:a permutations
Conditions: Full arrangement
Topic ideas
Recursive resolution. Note that Python takes a few bits of the list easily, shards ~ and then carefully discovers not only the full sort, but also the dictionary order (because each iteration is small to large, the traversal order depends on ~)
AC Code (PYTHON)
1 classsolution (object):2 defpermute (Self, nums):3 """4 : Type Nums:list[int]5 : Rtype:list[list[int]]6 """7 ifLen (nums) = = 1:8 return[Nums]9res = []Ten forIinchRange (len (nums)): One forJinchSelf.permute (Nums[:i] + nums[i+1:]): ARes.append ([nums[i]] +j) - returnRes
[Leetcode] (python): 046-permutations