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 ...
随机推荐
- AOP学习(2)
<property name="interceptorNames"> <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们 也可 ...
- PHP中include路径的解决方法汇总
这几天整理一份很乱的代码,这才意识到php对include处理不是一般的贱:别的编程语言在处理include中的相对目录时,都是以当前处理的文件作为基准.也就是说,如果A包含B,B包含C时,C再包含一 ...
- HashOperations
存储格式:Key=>(Map<HK,HV>) 1.put(H key, HK hashKey, HV value) putAll(H key, java.util.Map<? ...
- POJ1363 Rails 验证出栈序列问题
题目地址: http://poj.org/problem?id=1363 此题只需验证是否为合法的出栈序列. 有两个思路: 1.每个已出栈之后的数且小于此数的数都必须按降序排列.复杂度O(n^2),适 ...
- AngularJS学习笔记(二) 表单验证案例(ng-repeat/filter)
这一节相对来说需要理解的东西不是太多,记住了那些api就行了. 还是一个案例(同样来自miaov),一个表单验证,先上代码,然后再对对应的内容进行解释. <!DOCTYPE html> & ...
- python的random模块及加权随机算法的python实现
random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串. random.seed(x)改变随机数生成器的种子seed. 一般不必特别去设定seed,Python会自动选择seed. ...
- linux命令学习笔记(10):cat 命令
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示, 或者从标准输入读取内容并显示,它常与重定向符号配合使用. .命令格式: cat [选项] [文件] ...
- 3.1 第一个场景 HelloWorldScene
HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- OpenCV - Android Studio 中集成Opencv环境(包含opencv_contrib部分)
我在上一篇博客中说到了在Android中集成OpenCV,但是那个版本的OpenCV是没有SIFT和SURF算法的,因为这些算法是受专利保护的,所以并没有被包含在预编译库中,所以如果想要使用SIFT和 ...
- 洛谷P2895 [USACO08FEB]流星雨Meteor Shower
题目描述 Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will ...