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:给出一个数组(数组中的元素均不相同),求出这个数组 ...
随机推荐
- bootstrap基础学习一篇
官网:http://www.bootcss.com/ 这里,主要讲解bootstrap3.关于他的介绍就不用复述了. 1.示例 <!doctype html> <html lang= ...
- ThinkPHP项目笔记之数据库配置篇
对于配置文件,有几点说明 common:公共配置,也就是前台,后台,都可以调用的文件,具有普遍性 前台/后台:就是针对前后台的配置文件,具有针对性. 如:(公共文件基本配置) <?php ret ...
- 【代码备份】原图降采样后进行NLM滤波
文件路径: 滤波算法main.m: %% 测试函数 %NLM滤波及滤波与 clc,clear all,close all; ima_ori=double(imread('F:\Users\****n\ ...
- 如何在VS2010中更好的编写JavaScript代码
VS2010默认的JavaScript代码编辑器相对简单.对于大家熟悉的代码折叠,代码结构.函数导航,代码高亮等都不支持,使用很不便.下面介绍下我发现的几个VS2010插件,具有哪些功能,如何安装和使 ...
- Python学习笔记5-元组Tuple
tuple和list非常类似,但是tuple一旦初始化就不能修改,它也没有append(),insert()这样的方法.其他获取元素的方法和list是一样的 元组是用圆括号括起来的,其中的元素之间用逗 ...
- 遍历Map集合四中方法
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- Spring---Bean的继承与依赖
Spring 允许继承 bean 的配置(通过Bean的parent属性来指定,例如parent=”teacher“), 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean ...
- Exchange Powershell:Get-Counter (List connections to OWA )
使用方法: Get-CASActiveUsers -server server1,server2 Get-CASMailbox | Get-CASActiveUsers $RPC = Get-Coun ...
- 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则
负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...
- python AI(numpy,matplotlib)
http://blog.csdn.net/ywjun0919/article/details/8692018 apt-cache policy python-numpy sudo apt-get in ...