-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0046.py
51 lines (45 loc) · 1.57 KB
/
0046.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Source: https://leetcode.com/problems/permutations
# Title: Permutations
# Difficulty: Medium
# Author: Mu Yang <http://muyang.pro>
################################################################################################################################
# Given a collection of distinct integers, return all possible permutations.
#
# Example:
#
# Input: [1,2,3]
# Output:
# [
# [1,2,3],
# [1,3,2],
# [2,1,3],
# [2,3,1],
# [3,1,2],
# [3,2,1]
# ]
#
################################################################################################################################
class Solution:
"""Heap's algorithm"""
def permute(self, nums: List[int]) -> List[List[int]]:
yield from self.permuteInner(nums, len(nums))
def permuteInner(self, nums, k):
if k == 1:
yield list(nums)
else:
for i in range(k):
yield from self.permuteInner(nums, k-1)
if k % 2:
nums[i], nums[k-1] = nums[k-1], nums[i]
else:
nums[0], nums[k-1] = nums[k-1], nums[0]
################################################################################################################################
class Solution2:
def permute(self, nums: List[int]) -> List[List[int]]:
yield from self.permuteInner([], nums)
def permuteInner(self, prefix, nums):
if not nums:
yield prefix
else:
for i, n in enumerate(nums):
yield from self.permuteInner(prefix+[n], nums[:i]+nums[i+1:])