原题地址:https://oj.leetcode.com/problems/permutations-ii/

题意:

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2][1,2,1], and [2,1,1].

解题思路:这道题也是穷举全排列,只是集合中可能有重复的元素。分两步:1,对集合进行排序。2,进行剪枝,如果元素重复,直接跳过这一元素,决策树的这一枝被剪掉。

代码:

class Solution:
# @param num, a list of integer
# @return a list of lists of integers
def permuteUnique(self, num):
length = len(num)
if length == 0: return []
if length == 1: return [num]
num.sort()
res = []
previousNum = None
for i in range(length):
if num[i] == previousNum: continue
previousNum = num[i]
for j in self.permuteUnique(num[:i] + num[i+1:]):
res.append([num[i]] + j)
return res

[leetcode]Permutations II @ Python的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. leetcode -- Permutations II TODO

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. [Leetcode] Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  7. [leetcode]N-Queens II @ Python

    原题地址:https://oj.leetcode.com/problems/n-queens-ii/ 题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题. 解题思 ...

  8. [Leetcode] permutations ii 全排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. speech模块实现语音识别

    1.pip安装speech.pywin32 pip install speech pip install pywin32 2.例子 #!/usr/bin/python # coding:utf-8 f ...

  2. 火狐浏览器接口测试工具Poster

  3. BZOJ.3052.[WC2013]糖果公园(树上莫队 带修改莫队)

    题目链接 BZOJ 当然哪都能交(都比在BZOJ交好),比如UOJ #58 //67376kb 27280ms //树上莫队+带修改莫队 模板题 #include <cmath> #inc ...

  4. BZOJ2973 : 石头游戏

    考虑到$lcm(1,2,3,4,5,6)=60$,所以操作序列每60秒一个循环. 将操作表示成转移矩阵的形式,预处理出前60秒的转移矩阵以及它们的乘积$B$. 那么t秒的转移矩阵为前$t\bmod 6 ...

  5. Codeforces 666E Forensic Examination SAM+权值线段树

    第一次做这种$SAM$带权值线段树合并的题 然而$zjq$神犇看完题一顿狂码就做出来了 $Orz$ 首先把所有串当成一个串建$SAM$ 我们对$SAM$上每个点 建一棵权值线段树 每个叶子节点表示一个 ...

  6. securecrt中文乱码以及ubuntu设置locale

    参考文献 http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale http://www.bootf.com/547.html 强烈建议 ubuntu下面不 ...

  7. loading加载和layer.js

    layer.js中的loading加载 l本篇主要介绍layerjs中的loading加载在实际项目中的应用 1.使用的技术 前端:HTML5+CSS3+JS+layer.js 后端:.net 2.遇 ...

  8. [Go] panic 和 recover

    通常情况下,函数向其调用方报告错误的方式都是返回一个 error 类型的值.但是,当遇到致命错误的时候,很可能会使程序无法继续运行.这时,上述错误处理方式就太不适合了,Go 推荐通过调用 panic ...

  9. Unity3D实践系列11, 组件的添加和访问

    当把一个脚本附加到一个GameObject上的时候,这个GameObject就有了脚本组件. 通过GameObject的属性获取组件 比如如下: [RequireComponent(typeof(Ri ...

  10. Delphi 文件遍历

    unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...