本文共 819 字,大约阅读时间需要 2 分钟。
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]] """ ans = [] self.permute_helper(nums, 0, ans) return ans def permute_helper(self, nums, start, ans): if start == len(nums): ans.append(list(nums)) return for i in range(start, len(nums)): nums[i], nums[start] = nums[start], nums[i] self.permute_helper(nums, start+1, ans) nums[i], nums[start] = nums[start], nums[i]
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6243116.html,如需转载请自行联系原作者