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. 【Zookeeper】源码分析之持久化(三)之FileTxnSnapLog

    一.前言 前面分析了FileSnap,接着继续分析FileTxnSnapLog源码,其封装了TxnLog和SnapShot,其在持久化过程中是一个帮助类. 二.FileTxnSnapLog源码分析 2 ...

  2. js setTimeout setInterval 第三个参数说明

    1.api setTimeout: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout var timeoutID = ...

  3. k-means算法MATLAB和opencv代码

    上一篇博客写了k-means聚类算法和改进的k-means算法.这篇博客就贴出相应的MATLAB和C++代码. 下面是MATLAB代码,实现用k-means进行切割: %%%%%%%%%%%%%%%% ...

  4. SSM实战——秒杀系统前言

    项目来源:慕课网http://www.imooc.com/u/2145618/courses?sort=publish 项目开发流程:整合SSM框架——项目需求分析与实现——解决高并发优化 所用技术: ...

  5. J4架构应用过程中出现的问题与解决摘录

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6369346.html sendRedirect()的页面不能用EL表达式获取参数:因为是新的request.E ...

  6. 〖Android〗CyanogenMod同步错误的解决

    1. 错误信息: repo sync CyanogenMod/Superuser Fetching project CyanogenMod/Superuser Fetching projects: % ...

  7. 不同类型的磁盘存储在Ubuntu下的性能测试

    Ubuntu下通过lsusb判断USB存储是否是USB3.0: # 要查看Seagate这个移动硬盘 lsusb 或者 lsusb -t $ lsusb Bus Device : ID : Intel ...

  8. 基于springboot跟poi封装的最便捷的excel导出

    发布时间:2018-11-15   技术:springboot1.5.6 + maven3.0.5 + jdk1.8   概述 Springboot最便捷的Excel导出,只需要一个配置文件即可搞定 ...

  9. 解决打开bootstrap模态框抖动问题

    //打开模态框 function modalOpen(){ $('body').css("overflow", "hidden"); } //关闭模态框 fun ...

  10. 【jquery】ajax 动态 改变 select下拉框选中的值

    //JS<script type="text/javascript> //ajax动态给添加原料的[商品名称]下拉框绑定selected属性 $("#origin_co ...