[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 ...
随机推荐
- HDU4762(JAVA大数)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Yii2 框架下bootstrap 弹窗预览视频等~
Yii2 本身已经引用了'yii\bootstrap\BootstrapAsset',所以使用bootstrap 非常简洁. 1 在PHP页面引用命名空间 use app\assets\AppAsse ...
- convertView
[convertView] 参考:https://zhidao.baidu.com/question/423895201122905772.html
- 【转】android camera(四):camera 驱动 GT2005
关键词:android camera CMM 模组 camera参数 GT2005 摄像头常见问题 平台信息: 内核:linux系统:android 平台:S5PV310(samsung exyn ...
- 蚂蚁的难题(二)首尾相连数组的最大子数组和(DP)
蚂蚁的难题(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 下雨了,下雨了,蚂蚁搬家了. 已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种食材 ...
- HTML5 HybridApp开发上手指引
眼下我们项目是基于AngularJS和ionic框架开发的Hybrid App.支持android phone/pad, iPhone,iPad四个平台版本号.以及微信版. 由于使用的技术有点新,所以 ...
- 设置Android设备在睡眠期间始终保持WLAN开启的代码实现
MainActivity例如以下: package cc.ab; import android.os.Bundle; import android.provider.Settings; import ...
- LoadRunner性能测试中Controller场景创建需注意的几点
在LR工具做性能测试中,最关键的一步是Controller场景的设计,因为场景的设计与测试用例的设计相关联,而测试用例的执行,直接影响最终的测试结果是怎么的,因此,我们每设计一种场景,就有可能是一个测 ...
- linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程
关于登录linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...
- 理解MVC路由配置(转)
在上一篇文章中,我简短的谈了一下MVC的底层运行机制,如果对MVC还不是很了解的朋友,可以作为入门的参照.接下来,我开始介绍关于URL路由的相关知识.URL路由不是MVC独有的,相反它是独立于MVC而 ...