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的更多相关文章

  1. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  2. “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...

  3. 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)

    解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...

  4. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  5. 全排列12 · Permutations

    无重复 [抄题]: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have ...

  6. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  7. permutations and combinations

    # import itertools # # my_list = [1, 2, 3, 4, 5, 6] # # combinations = itertools.combinations(my_lis ...

  8. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

随机推荐

  1. Progressive Web App是一个利用现代浏览器的能力来达到类似APP的用户体验的技术——不就是chrome OS吗?

    什么是Progressive Web App? Progressive Web App是一个利用现代浏览器的能力来达到类似APP的用户体验的技术,由Google实现,让浏览器打开的网址像APP一样运行 ...

  2. 7 Python 数据类型—列表

    列表(list)是Python以及其他语言中最常用到的数据结构之一.Python使用使用中括号 [ ] 来解析列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置, ...

  3. L104

    marsh:It was like my own marsh country, flat and monotonous.The government will take more measures t ...

  4. 常用调试工具gdb,dbx,valgrind介绍一

    类UNIX下C/C++开发,代码调试比较麻烦,最原始的加跟踪调试很土,也很费时,特别是一个庞大的项目,代码行数非常大的时候调试起来非常费劲,当core dump时定位也不容易,这里介绍几个常用工具: ...

  5. SparkWriteToHFile

    1. HFile的LoadIncrement卡住 原来是因为权限,我一直以为,load函数之后是要删除文件的,但是hdfs://slave1:8020/test/info文件夹所有的是只读权限,而且考 ...

  6. poj 2105 IP Address(水题)

    一.Description Suppose you are reading byte streams from any device, representing IP addresses. Your ...

  7. HDOJ5438(图的各个连通分量遍历)

    #include<cstdio> #include<cstring> using namespace std; ; template<class T> struct ...

  8. Java中是构造器创建对象吗?

    首先,这里说明” Java中是构造器创建对象 “这句话是完全错误的. Java中构造器的作用主要是为了初始化变量的值...其实在执行构造器之前,Java对象所需要的内存空间,已经产生了... 一般可以 ...

  9. Java中继承的规则

    一.子类中对父类构造函数的调用规则 1.如果不显式指定,子类中的构造函数会默认调用父类中的无参构造 测试代码 package day07; import java.sql.Date; import j ...

  10. CSS是什么?W3C是什么?W3C盒子与IE盒子的区别?

    (1)层叠样式(Cascading Style Sheets, CSS)是用来表现HTML或XML文本样式的语言.   (2)W3C推荐规范(World Wide Web Consortium,W3C ...