Those who look up at the stars should not be laughed at
Title description
Given a sequence without repeated numbers, return all possible permutations.
Example:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
Source: LeetCode
Link: https://leetcode-cn.com/problems/permutations
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Problem solving ideas
Unique sequence is very simple, a maintenance vis
array will not be repeated take enough.
var permute = function (nums) {
let res = [];
let vis = {
};
let dfs = (t) => {
if (t.length == nums.length) {
res.push(t);
}
for (let i = 0; i < nums.length; i++) {
if (vis[i]) continue;
vis[i] = true;
t.push(nums[i]);
dfs(t.slice());
t.pop();
vis[i] = false;
}
}
dfs([]);
return res;
};
At last
The output of articles is not easy, and I hope you all support a wave!
Past selections:
Note Warehouse of Little Lion Front End
Visit Chaoyi's blog , which is convenient for friends to read and play~
学如逆水行舟,不进则退