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 ...
随机推荐
- 跟 Google 学 machineLearning [2] -- 关于 classifier.fit 的 warning
tensorfllow 的进化有点快.学习的很多例子已经很快的过时了,这里记录一些久的例子里被淘汰的方法,供后面参考. 我系统现在安装的是 tensorflow 1.4.1. 主要是使用了下面的代码后 ...
- ubuntu 下 cajview 替代方案
.caj 是知网提供的论文的标准格式,但是,知网只提供 win 版的工具.这里尝试了两个两个方案,均可行,做一下记录在此. 1. 使用 wine 版的 cajview pdf 可以正常用,但是 ca ...
- mint 设置无线 AP
所需软件: sudo apt-get install hostapd 1. 创建 hostapd 的 configure 文件 新建 hostapd.conf 文件,存放位置任意,与后面修改的路径一致 ...
- ReactNative踩坑日志——使用async/await语法解决网络请求的异步导致的指令执行顺序错乱问题
转载请注明原文地址: ReactNative的fetch是天然的异步请求,因此,如果你在一个代码块中使用了fetch,那么在执行的时候程序不会等待网络响应结束才执行下一条代码,而是会直接按顺序执行完整 ...
- js 正则表达式校验必须包含字母、数字、特殊字符
1.情景展示 在注册时,密码要求必须同时包含:字母.数字.特殊字符,如何实现? 2.原因分析 用正则表达式进行校验,是最方便的! 3.解决方案 // 密码必须由 8-64位字母.数字.特殊符号组成 ...
- 〖Android〗JDK7签名apk出现INSTALL_PARSE_FAILED_NO_CERTIFICATES的解决方法
由于某项需求,把JDK版本从JDK6升级到了JDK7: 但是签名APK之后出现了INSTALL_PARSE_FAILED_NO_CERTIFICATES的错误: 解决方法: 在签名时,添加参数 -di ...
- 【Linux】ssh建立隧道tunnel连接到内网设备
root@192.168.1.105 建立隧道: ssh -l root -N -f -R 9103:127.0.0.1:2222 work@11.11.13.17 解析:把本地127.0.0.1:2 ...
- V-rep学习笔记:vrep中的实用工具
在V-REP的模型浏览器中可以找到一个工具文件夹tools,点开后会在下面一栏中显示许多方框图,将这些方框拖到场景模型中可以实现一些特定的功能,方便建模或其它操作. Center of mass vi ...
- python模块之HTMLParser(原理很大程度上就是对类构造的熟练运用)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之HTMLParser(原理很大程度上就是对类构造的熟练运用) import HTMLPar ...
- C#程序实现窗体的最大化/最小化
C#程序实现窗体的最大化/最小化 http://blog.csdn.net/jiangqin115/article/details/41251215 private void button1_Clic ...