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. React-Native视频组件react-native-video使用(安卓版)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7366019.html 一:安装 在IDE中打开你的项目,然后在IDE的terminal,输入: npm i - ...

  2. sqlserver查询的结果复制到excel替换掉回车换行

    从sqlserver查询统计出的结果复制到excel,如果有回车,换行 ,或回车换行 ,复制到excel显示会乱会错版,查询的时候就替换掉回车换行,复制出来就不会乱了 ) ), ),),'') fro ...

  3. JS或AS中处理ARGB、RGBA颜色值时要小心

    (0xffffffff)>>24; //-1 (0xffffffff / 2)>>23; argb颜色需要一个无符号整数uint才能存储,如果首位是F,那么第一个二进制位就是1 ...

  4. python之模块pydoc

    # -*- coding: cp936 -*- #python 27 #xiaodeng import pydoc #主要用于从python模块中自动生成文档,这些文档可以基于文本呈现,也可以生成we ...

  5. DOM4J对于XML的用法

    一.基本使用方式     语法     1.获取根元素 Element root = document.getRootElement();     2.获取某个元素下的子元素 Element db_e ...

  6. Windows2008|2003超出最大连接数

    问题描述: 终端服务器超出最大允许连接数的解决方法 00.以管理员回话形式登录(本质踢掉他人) mstsc  /v:IP  /console mstsc /admin /v:ip mstsc /con ...

  7. 使用std::find_if提取序列容器的子串

    一个需求是这样的,一个vector容器中,我需要提取满足一定条件的元素的序列.就比如,一个树形结构,我把该接口拍扁成vector容器,每个节点都有一个惟一ID. 以下就是根据特定的ID查找节点下的子节 ...

  8. 【公众号】微信第三方登录(静默授权和非静默授权)(具体代码:U盘 新浪云SAE)

    一.微信联合登录是怎么登录的,有几种登录方式:微信联合登录和微信授权登录[授权登录(非静默授权)与静默授权] [主动授权]:需要用户确认登录,这样可以通过用户的个人确认,获取用户全面的信息,无论是否关 ...

  9. ios中 radioButton和DataPIcker,九宫格封装好使用

    下载地址 http://pan.baidu.com/share/link?shareid=2894506499&uk=923776187 引用这几个文件 radiobutton.封装好单选按钮 ...

  10. coreData详解

    1.初识CoreData CoreData的结构构成: NSManagedObjectModel的构成: 可以通过Entity创建继承自NSManagedObject类的文件,这个文件就是开发中使用的 ...