码迷,mamicode.com
首页 > 其他好文 > 详细

46. Permutations 排列

时间:2018-02-06 01:12:58      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:body   www   gray   span   title   append   end   sel   list   

 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]
]

  1. class Solution:
  2. def permute(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[List[int]]
  6. """
  7. res = []
  8. s = set(nums)
  9. def gen(l, added):
  10. if len(l) is len(nums):
  11. res.append(l[:])
  12. else:
  13. for i in s:
  14. if not i in added:
  15. l.append(i)
  16. added.add(i)
  17. gen(l, added)
  18. l.pop()
  19. added.remove(i)
  20. gen([], set())
  21. return res






46. Permutations 排列

标签:body   www   gray   span   title   append   end   sel   list   

原文地址:https://www.cnblogs.com/xiejunzhao/p/8419840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!