Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c508573

Browse files
add LeetCode 46. 全排列
1 parent 39ce780 commit c508573

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
6+
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
7+
8+
示例:
9+
10+
```javascript
11+
输入: [1,2,3]
12+
输出:
13+
[
14+
[1,2,3],
15+
[1,3,2],
16+
[2,1,3],
17+
[2,3,1],
18+
[3,1,2],
19+
[3,2,1]
20+
]
21+
```
22+
23+
来源:力扣(LeetCode)
24+
链接:https://leetcode-cn.com/problems/permutations
25+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
26+
27+
28+
29+
## 解题思路
30+
序列不重复就很简单了,维护一个 `vis`数组,不重复取就好了。
31+
32+
```javascript
33+
var permute = function (nums) {
34+
let res = [];
35+
let vis = {};
36+
let dfs = (t) => {
37+
if (t.length == nums.length) {
38+
res.push(t);
39+
}
40+
for (let i = 0; i < nums.length; i++) {
41+
if (vis[i]) continue;
42+
vis[i] = true;
43+
t.push(nums[i]);
44+
dfs(t.slice());
45+
t.pop();
46+
vis[i] = false;
47+
}
48+
}
49+
dfs([]);
50+
return res;
51+
};
52+
```
53+
54+
55+
## 最后
56+
文章产出不易,还望各位小伙伴们支持一波!
57+
58+
往期精选:
59+
60+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
61+
62+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
63+
64+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
65+
66+
```javascript
67+
学如逆水行舟,不进则退
68+
```
69+
70+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /