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. Promise实现子组件的多表单校验并反馈结果给父组件

    全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11529207.html,多谢,=.=~ 本文中多表单验证主要用到Promise.all()实现多 ...

  2. NOI2019网络同步赛游记

    我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...

  3. 『正睿OI 2019SC Day7』

    简单数论 质因子分解 素性测试 素性测试指的是对一个正整数是否为质数的判定,一般来说,素性测试有两种算法: \(1.\) 试除法,直接尝试枚举因子,时间复杂度\(O(\sqrt n)\). \(2.\ ...

  4. springmvc集成shiro后,session、request是否发生变化

    1. 疑问 我们在项目中使用了spring mvc作为MVC框架,shiro作为权限控制框架,在使用过程中慢慢地产生了下面几个疑惑,本篇文章将会带着疑问慢慢地解析shiro源码,从而解开心里面的那点小 ...

  5. Java自学-数字与字符串 字符串

    Java中的字符串String 示例 1 : 创建字符串 字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象 常见创建字符串手段: 每当有一个字面值出现的时候,虚拟机就会创 ...

  6. 把EXECL表格导入到WORD中

    一般我们在编写开发文档时需要进行表格导入导出,这里提供几种方法供参考. 法一: 打开EXECL,WORD软件,在需要导入表格的地方选择“插入” ,找到“对象选项: ”在对象对话框中点击“由文件创建”, ...

  7. [echart] webpack中安装和使用

    安装echart npm install echarts --save 全量引入 可以直接在项目代码中 require('echarts') 得到 ECharts. 官方示例 var echarts ...

  8. net start mysql 失败提示“NET HELPMSG 3534”

    lz使用的window系统8.0.16版本的mysql,以下四步解决如上问题: 1. mysqld -remove 2. mysqld --initialize 3.mysqld -install(m ...

  9. Executor的线程代码

    package com.open1111; import java.util.concurrent.ExecutorService;import java.util.concurrent.Execut ...

  10. C# 控制台定时器

    C# 定时器 关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System. ...