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 ...
随机推荐
- DBGrid和DBGridEH
二.应用实例 Enlib3.0组件包安装成功后 A.定制标题行 1.制作复杂标题行 标题行可设为2行以上高度,并可以为多列创建一个共同的父标题行.为实现这个效果,需在各个列标题属性中以“|”分隔父标题 ...
- C# 往excel出力数据
/// <summary> /// 出力Excel /// </summary> /// <param name="storeModelForExcel&quo ...
- mysql连接超时问题
前几天使用个脚本不停的查看redis队列中的事件.如果有则把事件取出来,然后进行一些数据库操作. 后来发现,每天的第一次有事件时都会到导致,找不到数据. 后来定位到问题,是mysql在连接长时间无活动 ...
- python-socket2
UDP,服务端 #! /usr/bin/env python #coding=utf-8 import socket #创建socket,指定ipv4,udp类型 s = socket.socket( ...
- AngularJS-Basic(一)
MVC:作为DataModel的$scope 依赖注入DI 模块化Module Service Filter Two way DateBinding Directive Unit Testing&am ...
- javaScript-进阶篇(三)
1.Window对象 window对象是BOM的核心,window对象指当前的浏览器窗口. window对象方法: 2.JavaScript 计时器 在JavaScript中,我们可以在设定的时间间隔 ...
- Mathf.Sin正弦
输入参数是弧度 Mathf.Sin public static float Sin(float f); Parameters Description Returns the sine of ang ...
- 汇编题目:按A键,当松开的时显示字母A
安装一个新的int9中断例程,功能:在DOS下,按下“A”键后,除非不再松开,如果松开,就显示满屏的“A”:其他的按键照常处理.提示:按下一个键时产生的扫描码称为通码,松开一个键时产生的扫描码称为断码 ...
- Linux keepalived与lvs的深入分析
一)概述 在本篇文章里,我们会涉及两部份内容,一个是LVS,另一个则是keepalived. 即我们用LVS和keepalived实现了负载均衡及高可用的服务器. LVS有实现三种IP负载均衡技术 ...
- java web基础学习 Forward和Redirect区别
Forward和Redirect代表了两种请求转发方式:直接转发和间接转发.对应到代码里,分别是RequestDispatcher类的forward()方法和HttpServletRequest类的s ...