File tree 2 files changed +61
-0
lines changed
2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments