leetcode: 47. Permutations II
来源:互联网 发布:天猫淘宝优惠卷代理 编辑:程序博客网 时间:2024/06/08 12:06
Q
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example
For example,[1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1]]
AC
# Time: O(n * n!)# Space: O(n)class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ solutions = [[]] for num in nums: next = [] for solution in solutions: for i in xrange(len(solution) + 1): candidate = solution[:i] + [num] + solution[i:] if candidate not in next: next.append(candidate) solutions = next return solutionsif __name__ == "__main__": assert Solution().permuteUnique([1, 1, 2]) == [[2, 1, 1], [1, 2, 1], [1, 1, 2]]
阅读全文
0 0
- [LeetCode]47.Permutations II
- LeetCode 47.Permutations II
- LeetCode --- 47. Permutations II
- [Leetcode] 47. Permutations II
- [leetcode] 47.Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II LeetCode
- Leetcode 47. Permutations II
- LeetCode 47. Permutations II
- LeetCode - 47. Permutations II
- [LeetCode] 47. Permutations II
- leetcode 47. Permutations II
- [LeetCode]--47. Permutations II
- [LeetCode]--47. Permutations II
- [leetcode] 47. Permutations II
- leetcode 47. Permutations II
- Leetcode-47. Permutations II
- [LeetCode]--47. Permutations II
- leetcode: 46. Permutations
- 32-多线程(wait和sleep的区别)1 2 33-多线程(停止线程方式-定义标记) 35-多线程(守护线程-setDaemon).1 2
- 40行JavaScript代码实现的3D旋转魔方动画效果
- 作业
- Cookie与Session机制
- leetcode: 47. Permutations II
- python小问题整合
- clisp 记录:5. 数据和控制流(Data and Control Flow)
- 一文深度揭秘3GPP:2G/3G/4G/Pre-5G标准化制定流程是这样的
- 36-多线程(其他方法-join 1 2 37--- 37-多线程(面试题)
- leetcode: 48. Rotate Image
- python调用C语言步骤
- leetcode: 49. Group Anagrams
- Java8内存模型