您的位置:首页 > 其它

Leetcode 46.Permutations(第十四周作业)

2018-01-10 14:49 405 查看
先贴原题:

Given a collection of distinct numbers, return all possible permutations.

For example, [1,2,3] have the following permutations:

[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]


这是一道典型的回溯题,通过不断递归即可找到所有解

class Solution(object):
def permute(self, nums):
""" :type nums: List[int] :rtype: List[List[int]] """
ret = []
def back(nums, p = []):
if not nums:
ret.append(p)
return
for i in range(len(nums)) :
back(nums[:i] + nums[i+1:], p + [nums[i]])
back(nums)
return ret
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: