题目来源


https://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].


题意分析
Input:list

Output:permutations

Conditions:跟上题类似,但是会有重复的元素


题目思路


直接用上题的做法,首先先排序,然后再遍历的时候用一个pre记录先前位置,如果当前位置与pre相同则跳过


AC代码(Python)


 class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
if len(nums) == 1:
return [nums]
res = []
pre = None
for i in range(len(nums)):
if nums[i] == pre:
continue
pre = nums[i]
for j in self.permuteUnique(nums[:i] + nums[i+1:]):
res.append([nums[i]] + j)
return res

[LeetCode]题解(python):047-Permutations II的更多相关文章

  1. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  2. LeetCode 047 Permutations II

    题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...

  3. Java for LeetCode 047 Permutations II

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

  4. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 047 Permutations II 有重复数字的全排列

    给定一个可能包含重复数字的集合,返回所有可能的不同全排列.例如,[1,1,2] 有以下不同全排列:[  [1,1,2],  [1,2,1],  [2,1,1]] 详见:https://leetcode ...

  6. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  7. LeetCode 题解 | 面试题57 - II. 和为s的连续正数序列

    题目描述 面试题57 - II. 和为s的连续正数序列 难度简单37收藏分享切换为英文关注反馈 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内 ...

  8. LeetCode题解之Unique Paths II

    1.题目描述 2.问题描述 使用动态规划算法,加上条件检测即可 3.代码 int uniquePathsWithObstacles(vector<vector<int>>&am ...

  9. Leetcode题解之Valid Palindrome II

    1.题目描述 2.问题分析 使用两个下标,检测下标对应的字符是否相等,若不相等,则考察子串. 3.代码 bool validPalindrome(string s) { , j = s.size()- ...

  10. LeetCode题解之Contains Duplicate II

    1.题目描述 2.题目分析 使用哈希表 和分情况讨论的方法 3.代码 bool containsNearbyDuplicate(vector<int>& nums, int k) ...

随机推荐

  1. cocos2d ccitemimage

    #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class H ...

  2. POJ 3352 (边双连通分量)

    题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...

  3. Zepto源码注释

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  4. BestCoder Round #74

    身败名裂啊...... T1WA了半天,30min才A. T2又WA了一发,然后Hack刚2min就被别人叉了. T3做完后最后40min不知所措. 去叉别人,看到一个人写D题判m=0很奇怪,随手把他 ...

  5. Sublime Text 2的快速入门和常用技巧

    1. 安装扩展器包管理器Package Control组件 点击菜单 View -> Show Console 调出控制台或者按快捷键 “Ctrl + `”(1左边的符号,可能和QQ拼音输入法和 ...

  6. Tomcat设置默认启动项目及Java Web工程设置默认启动页面

    Tomcat设置默认启动项目 Tomcat设置默认启动项目,顾名思义,就是让可以在浏览器的地址栏中输入ip:8080,就能访问到我们的项目.具体操作如下: 1.打开tomcat的安装根目录,找到Tom ...

  7. filter:alpha(opacity=100,style=1)

    filter:alpha(opacity=100,style=1) 1.opacity属性:设置透明度,取值0至100之间的任意数值,100表示完全不透明: 2.style属性:设置渐变风格: 0表示 ...

  8. Configuration of OpenCV2.1.0 with VS2010

    Add in the system Path: C:\Program Files (x86)\OpenCV-2.1.0\build\bin\Debug Project->Project Prop ...

  9. java 文件读写

    http://blog.csdn.net/jiangxinyu/article/details/7885518

  10. 强、软、弱、虚引用,ReferenceQueue,WeakHashMap

    强引用(Reference):所谓强引用就是普通引用.普通引用引用的对象,即使内存不足时,一般情况下也不会被回收. 软引用(weakReference):如果对象被且仅被软引用所引用时,内存不足时,会 ...