leetcode 46题 全排列
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
思路
回溯
代码
/**
* @param {number[]} nums
* @return {number[][]}
*/
function backTrack(nums, track, lists) {
if (track.length === nums.length) {
return lists.push([...track]);
}
for (let i=0; i<nums.length; i++) {
if (track.includes(nums[i])) continue;
track.push(nums[i]);
backTrack(nums, track, lists);
track.pop();
}
}
var permute = function(nums) {
const lists = [];
backTrack(nums, [], lists);
return lists;
};
复杂度分析
- 时间复杂度: O(N×N!))
- 空间复杂度: O(N)
FEATURED TAGS
前端开发
H5
JavaScript
设计模式
browser
jQuery
源码分析
生活
leetcode
Array
Stack
Queue
Linked List
剑指offer
Binary Search Tree
Binary Tree
Breadth-First Search
Depth-First Search
String
Set
Binary Search
Sliding Window
Backtracking
Dynamic Programming
Two Pointers
Union Find
Sort
Bit Operation
Recursion
Map
Graph
Search
Hash
LinkedList
复盘
QuickSort
Trie
Design
MinHeap
Traverse
Min Heap
Node.js
BackEnd
SQL
MySQL
Design Patterns
Network
计算机网络
Python
SVG