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. CDN存储和加速静态文件是什么回事(整理)(CDN是什么)

    CDN存储和加速静态文件是什么回事(整理)(CDN是什么) 一.总结 一句话总结: 内容分发网络:Content Delivery Network:依靠网络中的各个节点,就近发放静态资源. CDN的全 ...

  2. hdu 2955 Robberies(01背包)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. hdu 2050 折线分割平面(递推公式)

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. Selenium-webdriver基本操作1

    #! /usr/bin/env python #coding=utf-8 from selenium import webdriver import time print("====浏览器最 ...

  5. C++11特性 gcc源码包

    1.下载gcc最新的源码包  2.解压缩 tar -xf gcc-4.9.1.tar.gz 3. cd gcc-4.9.1 4.运行download_prerequisites脚本, ./contri ...

  6. python中zip()函数基本用法

    zip()函数接受一系列可迭代对象作为参数,将不同对象中相对应的元素打包成一个元组(tuple),返回由这些元组组成的list列表,如果传入的参数的长度不等,则返回的list列表的长度和传入参数中 ...

  7. B+树索引和哈希索引的明显区别是:

    如果是等值查询,那么哈希索引明显有绝对优势,因为只需要经过一次算法即可找到相应的键值:当然了,这个前提是,键值都是唯一的.如果键值不是唯一的,就需要先找到该键所在位置,然后再根据链表往后扫描,直到找到 ...

  8. poj 2390 Bank Interest(计算本利和)

    一.Description Farmer John made a profit last year! He would like to invest it well but wonders how m ...

  9. Poj1258_Agri-Net(最小生成树)

    一.Description(poj1258) Farmer John has been elected mayor of his town! One of his campaign promises ...

  10. SQL SERVER 中的*=和=*

    一.* =和= * 是在sql server2000中左连接,右连接的用法相当于left join 和right join,现在sql2005和2008默认是不支持的,可以设置兼容2000或2008 ...