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 ...
随机推荐
- Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- java sm3加密算法
java sm3加密算法实现 CreationTime--2018年7月13日09点28分 Author:Marydon 1.准备工作 所需jar包: bcprov-jdk15on-1.59.ja ...
- Ulua_toLua_基本案例(八)_LuaAccessingArray
Ulua_toLua_基本案例(八)_LuaAccessingArray using UnityEngine; using LuaInterface; public class AccessingAr ...
- Aspect实现对方法日志的拦截记录
在实际的业务系统中,我们通常都希望程序自动的打印方法的入参和返回值,某些特定的方法可能不想打印返回值(返回数据过大,打印日志影响效率),特有了下面的实现. 1.忽略返回值的java注解类 import ...
- Spring Boot动态修改日志级别
1. pom中引入 org.springframework.boot spring-boot-starter-actuator 2. 发送POST请求: 地址: http://[服务地址] ...
- maven正确的集成命令-U-B
http://healthandbeauty.iteye.com/blog/1618501 在持续集成服务器上使用怎样的 mvn 命令集成项目,这个问题乍一看答案很显然,不就是 mvn clean i ...
- Windows server 2012-remoteapp RDWEB修改默认端口
RDWEBl默认是通过3389端口调用remoteapp发布的应用程序.如果要修改该端口,可按下面的方式来修改: 1.修改mstsc远程连接的端口 http://www.cnblogs.com/rus ...
- PendingIntent传递数据注意参数RequestCode和Flag
数据发送方: public static void notify(Context context, TxrjMessage msg) { NotificationManager notifiM ...
- PLSQL Developer启动失败
原因:和打印服务冲突 禁掉打印服务,不过打印功能是不行了
- IDEA导入项目jar包红线, cannot resolve symbol xxxx问题
简单来说遇到的这种情况2种方式解决了, 不知道还有没有其他原因的. 1.reimport包 2.清缓存重启 针对1方法: a.确实不缺包: 可以先注释掉pom文件中的jar包, 此时idea会提示im ...