46. Permutations (全排列)
For example,[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
运用递归。 1234为例子
for i in 1234:
1 + 234(的全排列)
2 + 134(的全排列)
3 + 124(的全排列)
4 + 123 (的全排列)
对应程序的17行
class Solution(object):
def __init__(self):
self.res = [] def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.help(nums, 0, len(nums)) return self.res def help(self, a, lo, hi):
if(lo == hi):
self.res.append(a[0:hi])
for i in range(lo, hi):
self.swap(a, i, lo)
self.help(a, lo + 1, hi)
self.swap(a, i, lo)
def swap(self, a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp
非递归
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums.copy())
res = [nums.copy()]
n = self.next_permute(nums)
while True:
if n is None:
break
res.append(n)
n = self.next_permute(n.copy())
return res
def next_permute(self, a):
i = -1
for index in range(0, len(a) - 1)[::-1]:
if(a[index] < a[index + 1]):
i = index
break
if i==-1:
return None
min_i = a.index(min([k for k in a[i+1:] if k >a[i]]))
self.swap(a, i, min_i)
an = a[:i + 1] + self.revers(a[i + 1:])
return an
def revers(self, x):
for i in range(int(len(x) / 2)):
temp = x[i]
x[i] = x[len(x) - i - 1]
x[len(x) - i - 1] = temp
return x
def swap(self, a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp
46. Permutations (全排列)的更多相关文章
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- 46 Permutations(全排列Medium)
题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- 46. Permutations 排列数
46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
随机推荐
- identity_insert---实验性插入大批量数据和分页存储过程
OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable 1create database data_Test --创建数据库data_Test 2use da ...
- WPF 渲染级别 (Tier)
在WPF中,显卡的功能相差很大.当WPF评估显卡时,它会考虑许多因素,包括显卡上的RAM数量.对像素着色器(piexl shader)的支持(计算每个像素效果的内置程序,如透明效果),以及对顶点着色器 ...
- 匿名(无账号密码)从ftp服务器下载文件
public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...
- golang 之 channel
channel的机制是先进先出 无缓冲的channel: 如果你给channel赋值了,那么必须要读取它的值,不然就会造成阻塞. chreadandwrite :=make(chan int) cho ...
- 160307、Java调用Oracle存储过程返回结果集
一:无返回值的存储过程调用 存储过程: CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS BEGI ...
- node sever
一.基础创建服务器 // 引入Http var http = require("http"); // 创建服务器 http.createServer(function(reques ...
- Codeforces Round #427 (Div. 2)—A,B,C,D题
A. Key races 题目链接:http://codeforces.com/contest/835/problem/A 题目意思:两个比赛打字,每个人有两个参数v和t,v秒表示他打每个字需要多久时 ...
- 全量日志 requestId
常量参数和系统参数 API 的请求者不可见,由网关在请求后端服务时添加上. 常量参数.比如您的后端需要接收一个常量,但是这个常量您不希望被您的客户看见,那么就设置一个常量参数,可以在 Header 或 ...
- llvm,gcc
GCC,LLVM,Clang编译器对比 在XCode中,我们经常会看到这些编译选项(如下图),有些人可能会有些茫然,本文将对GCC4.2.LLVM GCC 4.2.LLVM compliler 2 ...
- 并发编程 - io模型 - 总结
1.提交任务得方式: 同步:提交完任务,等结果,执行下一个任务 异步:提交完,接着执行,异步 + 回调 异步不等结果,提交完任务,任务执行完后,会自动触发回调函数2.同步不等于阻塞: 阻塞:遇到io, ...