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. jquery vue 框架区别

    1.数据和视图分离,解耦 2.以数据驱动视图,只关心数据变化,DOM操作被封装

  2. RN API备忘

    1:Alert:启动一个提示对话框,包含对应的标题和信息. 2:Animated:动画效果. 3:AppRegistry:React Native应用的入口.应用的根组件应当通过AppRegistry ...

  3. merge-intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given[1,3],[2,6],[8,10 ...

  4. Spring DataSource>DBCP & C3P0

    Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...

  5. ScrollView嵌套EditText联带滑动的解决的方法

    本篇文章的相关内容需结合上文:从ScrollView嵌套EditText的滑动事件冲突分析触摸事件的分发机制以及TextView的简要实现和冲突的解决的方法 在说完了怎样解决ScrollView嵌套E ...

  6. XPages访问关系型数据库技术与最佳实践

    XPage 对于 Domino 开发人员的一大好处就是能够很方便和高效的访问关系型数据库.本文通过实例代码展现了在 XPage 中访问关系型数据库的具体步骤 , 同时讲解了一些在 XPage 中高效访 ...

  7. adb shell dumpsys的使用

    该命令用于打印出当前系统信息,默认打印出设备中所有service的信息,可以在命令后面加指定的service name. 有两种方法可以查看service list: 1. adb shell dum ...

  8. notepad++插件实现json、xml格式化

    notepad++比较出色的免费的数据编辑.格式化工具... 现在json.xml文件很流行.格式化也是必须的,方便查看关键信息! 01.下载notepad++及相关插件 npp_7.5.5-x86: ...

  9. 使用Adobe Audition 处理声音步骤

      软件: Adobe Audition 3.0 处理声音 插件:单独安装各种DirectX音效处理插件    一.录音 * 录音笔.手机 * Adobe Audition专业的录音软件   二..润 ...

  10. 多线程场景下如何使用 ArrayList

    ArrayList 不是线程安全的,这点很多人都知道,但是线程不安全的原因及表现,怎么在多线程情况下使用ArrayList,可能不是很清楚,这里总结一下. 1. 源码分析 查看 ArrayList 的 ...