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 ...
随机推荐
- Webwork【08】结合实战简析Controller 配置
虽然现在 MVC 框架层出不穷,但做为 Struts 前身的 webwork. 其经典程度不亚于贝利之于足球,双 11 之于淘宝特卖. 本篇将结合 webwork controller 配置文件 xw ...
- RN API备忘
1:Alert:启动一个提示对话框,包含对应的标题和信息. 2:Animated:动画效果. 3:AppRegistry:React Native应用的入口.应用的根组件应当通过AppRegistry ...
- OpenERP 7中 openerp-server.conf 的解释
服务器启动配置 – 通用项 程序代码: [选择] # Admin password for creating, restoring and backing up databases admin_pas ...
- 理解Android编译命令(转)
一.引言 关于Android Build系统,这个话题很早就打算整理下,迟迟没有下笔,决定跟大家分享下.先看下面几条指令,相信编译过Android源码的人都再熟悉不过的. source setenv. ...
- QTP Test ,VAPI-XP Test,LR Test 和ALM 集成远程分布式执行遇到的“access is denied ” “unspecified error”问题
大家都知道QTP与ALM (QC的升级版)集成是最好的一个分布式执行的结合.因为毕竟QTP是一个商业软件,HP当然不会让你去跟其他的open source的工具去集成,要不他到哪里去挣钱. 有时候服务 ...
- chrony 时间同步
RHEL7.4 192.168.100.1 作为时间服务器,其它主机到这台来同步时间. 时间服务器安装及配置:#yum install chrony --RHEL7默认已安装chrony,而没有安装n ...
- Windows遇到ERR_NETWORK_ACCESS_DENIED处理方案
问题描述: 用了总部vpn,总是打不开总部资源,之前可以一直提示,禁止访问互联网ERR_NETWORK_ACCESS_DENIED, 郁闷了好几天,今天自己查查资料解决了!说明,问题总是能解决的,只是 ...
- Linux-使用ctags、vim查看数据类型所在头文件
安装 ctags yum -y install ctags 生成 tags 文件 cd /usr/include ctags -R . 在 vim 中配置 tags #修改 vim 配置文件.或者使用 ...
- 安装apr报错rm: cannot remove `libtoolT': No such file or directory
直接打开/usr/local/src/apr-1.4.6/configure 把 $RM“$cfgfile” 那行删除掉 $RM“$cfgfile” 大约在 42302行 然后再重新运行 ./co ...
- 利用图片中的exif元数据批量查找图片中所包含的GPS信息
在图片的exif(交换图像文件格式)中标准定义了如何存储图像和音频文件的标准,而在这些标签中往往存在了一些容易被人们忽视却又重要的东西. 有一款工具名为exiftool,可以快速的解析所有标签,并将结 ...