[Algorithm] 46. Permutations
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的更多相关文章
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- [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 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- 46. Permutations 回溯算法
https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...
- LeetCode 【46. Permutations】
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- 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 ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
随机推荐
- Sitecore 8.2 扩展体验分析报告
本文简要介绍了如何为Experience Analytics创建自定义报告.在Sitecore术语中,我会说:创建新的报表维度和适当的报表以显示它们. 我们做的任务是:实现新的报告,显示不同网络浏览器 ...
- 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁
spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...
- 【java】单实例下的 流水号【21位】
单实例环境,不是分布式 需要流水号 /** * 流水号生成器 * * 年+天号+毫秒+随机数 * 2019+134+480+11位随机数 * 4+3+3+11 = 21位 * * * @author ...
- Python开发【第十五篇】模块的导入
的导入语句 import 语句 语法: import 模块名1 [as 模块别名] 作用: 将某模块整体导入到当前模块 示例: import math import sys,os 用法: 模块名.属性 ...
- List泛型用法(转载)
网上的List泛型用法,未验证,目测基本正确,教学用资料. 1. List的基础.常用方法: 声明: 1.List<T> mList = new List<T>(); T为列 ...
- WPF解决WindowsFormsHost背景透明
项目案例:WPF使用WindowsFormsHost播放视频,视频上显示边框.字幕等特效: 难点问题 1.WindowsFormsHost不支持背景透明: 2.WPF Panel.ZIndex无效,W ...
- python中用分别用selenium、requests库实现Windows认证登录
最近在搞单位的项目,实现python自动化,结果在第一步就把我给拒之门外,查资料问大佬,问我们开发人员,从周一折腾到周五才搞定了 接下给大家分享一下 项目背景:我们系统是基于Windows平台实现的, ...
- comet oj #7
A 签到题 题目描述 多次询问,每次询问给一个值域范围 [l,r][l,r],要回答下列四个问题: 从这个范围内选出两个整数(两个数可相同), (1) 这两个数的最小公倍数最大是多少? (2) 这两个 ...
- 浅谈Object.prototype.toString.call()方法
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object.对于null.array.function.o ...
- AMD规范中模块id的命名规则
AMD 即 Asynchronous Module Definition, 中文是“ 异步模块定义”的意思. AMD 规范制定了定义模块的规则,这样模块和模块的依赖可以被异步加载. AMD 规范只定义 ...