LeetCode Permutaions II
LeetCode解题之Permutaions II
原题
输出一个有反复数字的数组的全排列。
注意点:
- 反复数字的可能导致反复的排列
样例:
输入: nums = [1, 2, 1]
输出: [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
解题思路
这道题是上一题 Permutations 的加强版,如今要考虑反复的数字了,採用了偷懒的办法,先把数组排序。遍历时直接无视反复的数字,在原来的基础上仅仅要加入两行代码。
AC源代码
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
nums.sort()
self.get_permute([], nums, result)
return result
def get_permute(self, current, num, result):
if not num:
result.append(current + [])
return
for i, v in enumerate(num):
if i - 1 >= 0 and num[i] == num[i - 1]:
continue
current.append(num[i])
self.get_permute(current, num[:i] + num[i + 1:], result)
current.pop()
if __name__ == "__main__":
assert Solution().permuteUnique([1, 2, 1]) == [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源代码。
LeetCode Permutaions II的更多相关文章
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] H-Index II 求H指数之二
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode H-Index II
原题链接在这里:https://leetcode.com/problems/h-index-ii/ 题目: Follow up for H-Index: What if the citations a ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode——N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
随机推荐
- ssh 防止超时掉线
超时掉线的机制原始驱动力是什么?反正远程操作久置掉线确实挺烦的. 解决的办法呢,也是有的,客户端和服务器端都可以做.就是使用 no-op 反空闲协议发送呼吸包,来确认另一端是否在线:没回应就下线,而不 ...
- Java中经常使用缓存Cache机制的实现
缓存,就是将程序或系统常常要调用的对象存在内存中,一遍其使用时能够高速调用,不必再去创建新的反复的实例. 这样做能够降低系统开销.提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文件 ...
- ROS中发布激光扫描消息
激光雷达工作时会先在当前位置发出激光并接收反射光束,解析得到距离信息,而后激光发射器会转过一个角度分辨率对应的角度再次重复这个过程.限于物理及机械方面的限制,激光雷达通常会有一部分“盲区”.使用激光雷 ...
- 特殊字符导致json字符串转换成json对象出错
在对数据库取出来的数据(特别是描述信息)里面含有特殊字符的话,使用JSON.parse将json字符串转换成json对象的时候会出错,主要是双引号,回车换行等影响明显,左尖括号和右尖括号也会导致显示问 ...
- PHP5.4 需要添加的configure参数
./configure --prefix=/usr/local/php --with-gd=/usr/local/gd --with-jpeg-dir=/usr/local/jpeg --with-p ...
- Spring Cloud Eureka集群 动态扩展新节点
场景描述: Eureka的集群节点有两个,互相注册形成集群,已经支持动态刷新(不知道怎么让Eureka支持动态刷新的可以参考http://www.cnblogs.com/flying607/p/845 ...
- springboot+mybatis+maven角色权限框架
发布时间:2018-10-24 技术:springboot,mybatis,maven,shiro 概述 Springboot作为基础框架,使用mybatis作为持久层框架 使用官方推荐的th ...
- 基于RESTful API 怎么设计用户权限控制?
前言 有人说,每个人都是平等的:也有人说,人生来就是不平等的:在人类社会中,并没有绝对的公平,一件事,并不是所有人都能去做:一样物,并不是所有人都能够拥有.每个人都有自己的角色,每种角色都有对某种资源 ...
- Mac OS使用技巧十九:Safari碉堡功能之二查看网页源代码
由于大三下的时候选修了搜索技术.了解了网络上搜索引擎和网络爬虫的信息扒取的一些东西,后来我们做了一个比較水的东西.就是仅仅扒取了几家较大的下载站点几十个软件的评分下载量等信息,当用户输入一个 ...
- MyEclipse 2014安装properties文件插件
安装步骤 1.下载PropertiesEditor插件 http://propedit.sourceforge.jp/index_en.html2.解压出features.plugins文件3.在My ...