[Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 47: Permutations II
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]. ===Comments by Dabay===
先排序,然后DFS。
注意去重,如果进入了一次DFS,用pre记录上次用的数字。这个pre只在DFS中的for中有效,所以每次进入进入新的DFS的时候,重新设置为None.
''' class Solution:
# @param num, a list of integer
# @return a list of lists of integers
def permuteUnique(self, num):
res = []
num.sort()
self.DFS(res, [], None, num)
return res def DFS(self, res, l, pre, nums):
if len(nums) == 0:
res.append(list(l))
pre = None
for i in xrange(len(nums)):
if nums[i] == pre:
continue
l.append(nums[i])
self.DFS(res, l, nums[i], nums[:i] + nums[i+1:])
l.pop()
pre = nums[i] def main():
sol = Solution()
nums = [1,1,2]
print sol.permuteUnique(nums) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]47: Permutations II的更多相关文章
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode OJ 47. Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
随机推荐
- MFC与Qt的内存管理
最近在做MFC向Qt的移植,在内存管理方面遇到了很头疼的问题,虽然不知道问题到底出在哪,先了解下这两个库的内存管理方式.于是转载两篇关于内存管理的文章. 一. Qt内存管理: 在Qt的程序中经常会看到 ...
- libevent for qt网络模块
libevent for qt网络模块,直接替换qt的select模型,支持epoll,select,pool.使用非常简单,无需修改以前的代码结构 最近在开发im服务器,需要大并发链接.QT默认的是 ...
- 《how to design programs》第11章自然数
这章让我明白了原来自然数的定义本来就是个递归的过程. 我们通常用枚举的方式引出自然数的定义:0,1,2,3,等等(etc).最后的等等是什么意思?唯一能把等等从描述自然数的枚举方法中去除的方法是自引用 ...
- PL/SQL --> 动态SQL调用包中函数或过程
动态SQL主要是用于针对不同的条件或查询任务来生成不同的SQL语句.最常用的方法是直接使用EXECUTE IMMEDIATE来执行动态SQL语句字符串或字符串变量.但是对于系统自定义的包或用户自定的包 ...
- location.href IE6 下不起作用的罪魁祸首
解决问题 在btn_publish函数内逻辑最后面加:return false;
- Visual Studio® 2010 Web Deployment Projects站点编译生成bin同时发表插件
VS2010环境下: 1.Visual Studio® 2010 Web Deployment Projects下载地址: http://www.microsoft.com/downlo ...
- Unity NGUI 血条制作
NGUI 血条制作步骤 实现过程: 模拟血条的变化当点击按钮Button是血条会实时发生变化. 1.向Unity中导入NGUI2.6.3.unitypackage 点击create your ui 后 ...
- 【贪心】【uva11520】 Fill the Square
填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...
- 使用SOAPUI测试WEBAPI接口
其实很简单,用起来也挺方便的. 首先,先去百度下SoapUI 下载,下载安装好后,打开软件. 1.新建一个项目 点击 Create Empty Project 按钮后,会自动新建一个项目,名字默认为: ...
- javascript模式——Decorator
Decorator 模式是一种结构型模式,他意在促进代码的复用,是塑造子类的一个方式. 这种想法是基于,新增的属性,对于对象来说不是必须的基本功能.我们为特殊的对象添加自己的方法,而不是重新创建一个类 ...