Skip to content

Commit 470106c

Browse files
author
wabiwabo
committed
添加全排列
1 parent af63f4c commit 470106c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

20.有效的括号.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=20 lang=javascript
3+
*
4+
* [20] 有效的括号
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {boolean}
11+
*/
12+
var isValid = function (s) {
13+
const map = new Map();
14+
map.set(")", "(")
15+
map.set("]", "[")
16+
map.set("}", "{")
17+
18+
const stack = [];
19+
for (let i of s) {
20+
if (map.has(i)) {
21+
const current = stack.pop();
22+
if (current !== map.get(i)) return false;
23+
} else {
24+
stack.push(i);
25+
}
26+
}
27+
return stack.length === 0;
28+
};
29+
// @lc code=end
30+

46.全排列.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=46 lang=typescript
3+
*
4+
* [46] 全排列
5+
*/
6+
7+
// @lc code=start
8+
function permute(nums: number[]): number[][] {
9+
const res:number[][] = [];
10+
const path: number[] = [];
11+
const visited = new Array(nums.length).fill(false);
12+
13+
function DFS(path, visited){
14+
if(path.length === nums.length) {
15+
res.push([...path]);
16+
return
17+
}
18+
for(let i = 0; i < nums.length;i++){
19+
if(visited[i]) continue;
20+
path.push(nums[i]);
21+
visited[i] = true;
22+
DFS(path, visited)
23+
path.pop();
24+
visited[i] = false;
25+
}
26+
}
27+
DFS(path, visited);
28+
return res;
29+
};
30+
// @lc code=end
31+

0 commit comments

Comments
 (0)