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的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. [LeetCode] Permutations II 全排列之二

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

  3. [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 ...

  4. [LeetCode] H-Index II 求H指数之二

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  5. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  7. LeetCode H-Index II

    原题链接在这里:https://leetcode.com/problems/h-index-ii/ 题目: Follow up for H-Index: What if the citations a ...

  8. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  9. LeetCode——N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

随机推荐

  1. http2.0 特性

    1.HTTP 2.0将只用于https://网址,而 http://网址将继续使用HTTP/1. 查看http协议(chrome F12) 2.异步连接多路复用 HTTP2.0 把消息分解为独立帧,交 ...

  2. 【Oracle】RAC下的一些经常使用命令(一)

    节点层: olsnodes -n:显示每一个节点编号. [oracle@rac1 ~]# olsnodes -n rac1    1 rac2    2 -p:显示每一个节点用于private int ...

  3. PhpStorm 2017.1安装及破解过程

    1.下载原安装文件和破解文件:magnet:?xt=urn:btih:7FD357910B7AFBFC8FBA3647A2FAC8F15F0D02F0  (直接放到迅雷里下载) 2.Licence S ...

  4. Porting .Net RSA xml keys to Java

    byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim())); byte[] modBytes = Base64.deco ...

  5. 【Linux】&、&&、|、||的用法说明

    在Linux系统中,&.&&.|.||的用法如下: &  表示任务在后台运行,例如:ping 127.0.0.1>a.txt& &&  表 ...

  6. java第三节 面向对象(上)

    //第三讲 //面向对象(上) /* 理解面向对象的概念 面向过程 在一个结构体中定义窗体的大小,位置,颜色,背景等属性,对窗口操作的函数窗口本身的定义没有任何关系 如HideWindow, Move ...

  7. DOM API详解

    来源于:http://zxc0328.github.io/2016/01/23/learning-dom-part1/ https://zxc0328.github.io/2016/01/26/lea ...

  8. 基于swagger进行接口文档的编写

    0. 前言 近期忙于和各个银行的代收接口联调,根据遇到的问题,对之前编写的接口进行了修改,需求收集和设计接口时想到了方方面面,生产环境下还是会遇到意想不到的问题,好在基本的执行逻辑已确定,因此只是对接 ...

  9. NSDictionary的分类

    @implementation NSDictionary (extra) //根据key值的到字典中的object - (id)getObjectByKey:(NSString*)key { NSAr ...

  10. Linux-静态库生成

    Linux上的静态库,其实是目标文件的归档文件.在Linux上创建静态库的步骤如下: 写源文件,通过 gcc -c xxx.c 生成目标文件. 用 ar 归档目标文件,生成静态库. 配合静态库,写一个 ...