Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

The idea is using recursive approach, we can define a recursive function

helper(prefix, suffix, result)

Where the prefix is: for example

[1]

suffix:

[2, 3]

So every time, we take one value from suffix, put into prefix.

The stop logic is that when suffix length is zero, then we push the prefix into result.

if (sfx.length === 0) {
result.push(pfx);
}
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
function helper (pfx, sfx, result) {
if (sfx.length === 0) {
result.push(pfx);
} else {
const len = sfx.length;
for (let i = 0; i < len; i++) {
helper(
pfx.concat(sfx[i]),
sfx.slice(0, i).concat(sfx.slice(i+1, sfx[len])),
result
);
}
} return result;
} return helper([], nums, []);
}; permute([1,2,3]) /* [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] */
[], [1,2,3]

[1], [2,3]

[1, 2], [3], [] | [1, 3], [2] 

---> [1,2,3] | [1,2,3]

[2], [1, 3]
[2, 1], [3] | [2,3] [1] --> [2,1,3] | [2,3,1] [3], [1,2]
[3,1][2] | [3,2], [1] --> [3,1,2] | [3,2,1]

[Algorithm] 46. Permutations的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  3. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  4. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  5. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. 46. Permutations 回溯算法

    https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...

  7. LeetCode 【46. Permutations】

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  8. 46. Permutations——本质和树DFS遍历无异 fun: for i in nums fun(i)

    Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have ...

  9. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

随机推荐

  1. sql语句递归查询(start with)

    在做项目中遇到一个问题,就是同一个表中的数据存在级联关系,但是只要查出来的末级数据,纠结了好久,好不容易找到了一个博主的分享,在这里做个记录,也是和大家一起分享学习一下这位大神的技术,共勉 写代码时碰 ...

  2. 什么是SQL ?

    SQL 1.什么是SQL ? Structured Query Languange:结构化查询语言 其实就是定义了操作所有关系型数据库的规则.每一种数据库操作的方式存在不一样的地方,称为“方言”. 2 ...

  3. Elastic Stack 7.5.0白金版永不过期

    适用版本:7.4.0~7.5.0 警告:本文章仅限于学习,非商业用途. 目录结构 # 先创建相关目录,具体结构如下: /opt |-- bulid # 编译目录 | |- src |-- instal ...

  4. Winform串口编程---接收数据demo(VSPD虚拟串口)

    参考地址:https://blog.csdn.net/memgxingfeixiang/article/details/52513970  https://blog.csdn.net/kevin_io ...

  5. Socker编程之UDP

    一:socket简介 1. 不同电脑上的进程之间如何通信 首要解决的问题是如何唯一标识一个进程,否则通信无从谈起! 在1台电脑上可以通过进程号(PID)来唯一标识一个进程,但是在网络中这是行不通的. ...

  6. drf--ModelSerializers序列化

    目录 drf--ModelSerializers序列化 项目准备 配置 settings.py 路由 多表设计 表关系分析 创建models 模型序列化 自定义模型序列化 api/serializer ...

  7. 英语apyrite红碧玺apyrite单词

    红碧玺(apyrite)是粉红.桃红.玫瑰红.深红.紫红等以红色调为主的碧玺,矿物学上主要属于锂电气石和镁电气石.红色起因可能与微量锰及锂和铯有关. 红色是碧玺中价值最高的,其中以紫红色和玫瑰红色最佳 ...

  8. MES系统帮助冷轧厂实现次品的流程解析

    为了解决现场实际生产过程中纸质不良品卡片易丢失.周期长.传递缓慢,不能起到质量警示和生产预警等诸多方面的问题,某冷轧厂采取了在MES系统中实现不良品业务流程的方案,完全替代和取消了纸质不良品卡片,在M ...

  9. tomcat启动Publishing failed with multiple errors

    转自:https://blog.csdn.net/leisurelen/article/details/46940441 新安装一个tomcat插件.启动的时候就弹错误框.但tomcat还能使用. P ...

  10. fiddler模拟弱网测试

    1.首先设置手机代理 设置手机代理到本机ip,端口号8888(Fiddler默认设置): 手机访问http://ip:port安装Fiddler证书 2.修改fiddler配置 勾选上后,已经开始限速 ...