-
Notifications
You must be signed in to change notification settings - Fork 43
/
permutations.py
246 lines (216 loc) · 6.46 KB
/
permutations.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
46. Permutations
Medium
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
"""
# V0
# IDEA : BACKTRACK,
# similar idea as LC 77 -> difference : contains VS start
class Solution(object):
def permute(self, nums):
def help(cur):
if len(cur) == n_len:
if cur not in res:
res.append(list(cur))
return
if len(cur) > n_len:
return
for i in nums:
#print ("i = " + str(i) + " cur = " + str(cur))
if i not in cur:
cur.append(i)
help(cur)
"""
NOTE !!! : we UNDO the last op we just made (pop last element we put into array)
"""
cur.pop(-1)
# edge case
if not nums:
return [[]]
n_len = len(nums)
res = []
help([])
#print ("res = " + str(res))
return res
# V0
class Solution(object):
def permute(self, nums):
visited = [0] * len(nums)
res = []
path = []
self.dfs(path)
return res
def dfs(self, path):
if len(path) == len(nums):
res.append(path)
else:
for i in range(len(nums)):
if not visited[i]:
visited[i] = 1
dfs(path + [nums[i]])
visited[i] = 0 ### to check if "visited[i] = 0" is necessary
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79363903
# IDEA : VIA permutations FUNC
from itertools import permutations
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
return list(permutations(nums))
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79363903
# IDEA : DFS + RECURSION
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.dfs(nums, res, [])
return res
def dfs(self, nums, res, path):
if not nums:
res.append(path)
else:
for i in range(len(nums)):
self.dfs(nums[:i] + nums[i + 1:], res, path + [nums[i]])
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79363903
# IDEA : BACKTRACKING + DFS
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
visited = [0] * len(nums)
res = []
def dfs(path):
if len(path) == len(nums):
res.append(path)
else:
for i in range(len(nums)):
if not visited[i]:
visited[i] = 1
dfs(path + [nums[i]])
visited[i] = 0
dfs([])
return res
# V1'''
# https://www.jiuzhang.com/solution/permutations/#tag-highlight-lang-python
# IDEA : Recursion
class Solution:
"""
@param nums: A list of Integers.
@return: A list of permutations.
"""
def permute(self, nums):
# write your code here
def _permute(result, temp, nums):
if nums == []:
result += [temp]
else:
for i in range(len(nums)):
_permute(result, temp + [nums[i]], nums[:i] + nums[i+1:])
if nums is None:
return []
if nums is []:
return [[]]
result = []
_permute(result, [], sorted(nums))
return result
# V1''''
# https://www.jiuzhang.com/solution/permutations/#tag-highlight-lang-python
# IDEA : Non-Recursion
class Solution:
"""
@param nums: A list of Integers.
@return: A list of permutations.
"""
def permute(self, nums):
if nums is None:
return []
if nums == []:
return [[]]
nums = sorted(nums)
permutation = []
stack = [-1]
permutations = []
while len(stack):
index = stack.pop()
index += 1
while index < len(nums):
if nums[index] not in permutation:
break
index += 1
else:
if len(permutation):
permutation.pop()
continue
stack.append(index)
stack.append(-1)
permutation.append(nums[index])
if len(permutation) == len(nums):
permutations.append(list(permutation))
return permutations
# V2
# Time: O(n * n!)
# Space: O(n)
class Solution(object):
# @param num, a list of integer
# @return a list of lists of integers
def permute(self, num):
result = []
used = [False] * len(num)
self.permuteRecu(result, used, [], num)
return result
def permuteRecu(self, result, used, cur, num):
if len(cur) == len(num):
result.append(cur[:])
return
for i in range(len(num)):
if not used[i]:
used[i] = True
cur.append(num[i])
self.permuteRecu(result, used, cur, num)
cur.pop()
used[i] = False
# Time: O(n^2 * n!)
# Space: O(n^2)
class Solution2(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.dfs(nums, [], res)
return res
def dfs(self, nums, path, res):
if not nums:
res.append(path)
for i in range(len(nums)):
# e.g., [1, 2, 3]: 3! = 6 cases
# idx -> nums, path
# 0 -> [2, 3], [1] -> 0: [3], [1, 2] -> [], [1, 2, 3]
# -> 1: [2], [1, 3] -> [], [1, 3, 2]
#
# 1 -> [1, 3], [2] -> 0: [3], [2, 1] -> [], [2, 1, 3]
# -> 1: [1], [2, 3] -> [], [2, 3, 1]
#
# 2 -> [1, 2], [3] -> 0: [2], [3, 1] -> [], [3, 1, 2]
# -> 1: [1], [3, 2] -> [], [3, 2, 1]
self.dfs(nums[:i] + nums[i+1:], path + [nums[i]], res)