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 ...
随机推荐
- web前端开发,如何提高页面性能优化?
内容方面: 1.减少 HTTP 请求 (Make Fewer HTTP Requests) 2.减少 DOM 元素数量 (Reduce the Number of DOM Elements) 3.使得 ...
- Spring Boot 之 RESTfull API简单项目的快速搭建(三)
1.运行打包后的项目 java -jar spring-boot-demo-2-1-0.0.1-SNAPSHOT.jar
- uva 11346 - Probability(概率)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">题目链接:uva 11346 - ...
- SSH黄金参数
ssh -o ConnectTimeout=3 -o ConnectionAttempts=5 -o PasswordAuthentication=no -o StrictHostKeyCheckin ...
- 代理服务 SQUID 测试
第一部分:SQUID基础 Squid代理服务的基本配置: http_port 3128 #设置监听的IP与端口号 cache_mem 64 MB ...
- 【mysql】数据库Schema的优化
由于MySQL数据库是基于行(Row)存储的数据库,而数据库操作 IO 的时候是以 page(block)的方式,也就是说,如果我们每条记录所占用的空间量减小,就会使每个page中可存放的数据行数增大 ...
- WordPress固定链接修改后访问文章页面404
如题, 修改固定链接为自定义结构后, 访问文章页面出现404的nginx错误. 解决:修改nginx.conf配置文件(/usr/local/nginx/conf/nginx.conf). 在serv ...
- zabbix数据库需要多大硬盘?我告诉你
本次案例:100台服务器,每台服务器有30个监控项,每个监控项60秒刷新一次,需要多大的硬盘呢?众所周知,zabbix基本都是通过web配置,这些配置数据也是存放到数据库里的,但是它对硬盘容量的要求基 ...
- List<String> bikeList = Arrays.asList(bikeuuids);
最近项目中 List<String> bikeList = Arrays.asList(bikeuuids);报错 而且console里面没有特别有用的 bikeList.add(&qu ...
- 基于js利用经纬度进行两地的距离计算(转)
转自:http://www.storyday.com/html/y2009/2212_according-to-latitude-and-longitude-distance-calculation- ...