Permutations and Permutations II
Permutations
问题:给定一个无重复元素的数组,输出其中元素可能的所有排列
示例:
输入:[2,3,4]
输出:[
[2,3,4],
[2,4,3],
[3,2,4],
[3,4,2],
[4,2,3],
[4,3,2]
]
解决思路:循环加递归
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.out = []
self.per(nums)
return self.out def per(self,nums,one_per=[]):
if not nums:
self.out.append(one_per)
return
for i in nums:
remain = nums[:]
remain.remove(i)
self.per(remain,one_per+[i])
Permutations II
问题:给定一个可能带有重复元素的数组,输出其元素可能的所有排列,不能重复输出
示例:
输入:[1,2,1]
输出:[
[1,1,2],
[1,2,1],
[2,1,1]
]
Python代码:
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.out = []
nums.sort()
self.per(nums)
return self.out def per(self,nums,one_per=[]):
if not nums:
self.out.append(one_per)
return
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i-1]:
continue
self.per(nums[:i]+nums[i+1:],one_per+[nums[i]])
Permutations and Permutations II的更多相关文章
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...
- 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence
▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...
- 全排列12 · Permutations
无重复 [抄题]: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have ...
- Python itertools.combinations 和 itertools.permutations 等价代码实现
最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...
- permutations and combinations
# import itertools # # my_list = [1, 2, 3, 4, 5, 6] # # combinations = itertools.combinations(my_lis ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
随机推荐
- php数据结构课程---3、队列(队列实现方法)
php数据结构课程---3.队列(队列实现方法) 一.总结 一句话总结: 1.数据实现:适用于功能不复杂的情况 2.链表实现:受限链表,只能队头队尾操作:适用于功能复杂情况 1.队列的数组实现注意点? ...
- SQL Server convet函数data_type默认长度
语法 CONVERT(data_type(length),data_to_be_converted,style) data_type(length) 规定目标数据类型(带有可选的长度),默认30个字符 ...
- objdump 命令的用法
gcc命令之 objdump ---------------objdump是用查看目标文件或者可执行的目标文件的构成的GCC工具---------- 以下3条命令足够那些喜欢探索目标文件与源代码之间的 ...
- 【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Spring MVC 学习第一篇
很好的MVC 参考blog:http://jinnianshilongnian.iteye.com/blog/1752171 MVC: 概念:是一种设计模式,并没有引入新的技术,只是把我们开发的结构组 ...
- TypeError: 'str' object is not callable
Python报错TypeError: 'str' object is not callable
- LOJ2305 「NOI2017」游戏
「NOI2017」游戏 题目背景 狂野飙车是小 L 最喜欢的游戏.与其他业余玩家不同的是,小 L 在玩游戏之余,还精于研究游戏的设计,因此他有着与众不同的游戏策略. 题目描述 小 L 计划进行$n$场 ...
- 利用src.rpm包修改源码后重新制作rpm包
1. 下载 .src.rpm包.例:https://repos.fedorapeople.org/repos/openstack/EOL/openstack-grizzly/epel-6/openst ...
- TS学习之Symbol
symbol成为了一种新的原生类型,就像number和string一样(意思是可以像string一样,作为对象的属性等) Symbols是不可改变且唯一的 //symbol通过Symbol函数构造,但 ...
- numpy.zeros(shape, dtype=float, order='C')
numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...