-
Notifications
You must be signed in to change notification settings - Fork 0
/
046_permutations.py
48 lines (45 loc) · 1.28 KB
/
046_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/3/15
'''
这种递归题,对于现在的我来说,还是比较难啊。
觉得能独自写出这种代码的人还是很厉害的。最起码我现在写不出来。
'''
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums]
res = []
for i in range(len(nums)):
prefix = nums[i]
rest = nums[:i] + nums[i+1:]
for j in self.permute(rest):
res.append([prefix]+j)
return res
class Solution1(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums]
res = []
for i in range(len(nums)):
perfix = nums[i]
rest = nums[:i] + nums[i + 1:]
for j in self.permute(rest):
res.append([perfix] + j)
return res
if __name__ == '__main__':
nums = [1,2,3,4]
print(Solution().permute(nums))