File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33 * @return {boolean }
44 */
55var isValid = function ( s ) {
6-
7- const tempArray = [ ] ;
8- const pairObject = {
6+ const bracketsObj = {
97 ')' : '(' ,
108 '}' : '{' ,
119 ']' : '['
1210 }
11+ const stackArray = [ ] ;
12+
13+ for ( let i = 0 ; i < s . length ; i ++ ) {
1314
14- for ( const ch of s ) {
15- if ( ch === '(' || ch === '{' || ch === '[' ) {
16- tempArray . push ( ch ) ;
15+ let currentBracket = s [ i ] ;
16+ let isRightBracket = currentBracket in bracketsObj ;
17+
18+ if ( ! isRightBracket ) {
19+ stackArray . push ( currentBracket ) ;
20+ continue ;
21+ }
22+
23+ let isPair = stackArray [ stackArray . length - 1 ] == bracketsObj [ currentBracket ] ;
24+
25+ if ( isPair ) {
26+ stackArray . pop ( ) ;
1727 } else {
18- if ( tempArray . pop ( ) !== pairObject [ ch ] ) {
19- return false ;
20- }
28+ return false ;
2129 }
2230 }
2331
24- return tempArray . length === 0 ;
32+ return stackArray . length === 0
33+
2534} ;
2635
2736
37+ // -----6기 활동 시의 풀이----
38+ // /**
39+ // * @param {string } s
40+ // * @return {boolean }
41+ // */
42+ // var isValid = function (s) {
43+
44+ // const tempArray = [];
45+ // const pairObject = {
46+ // ')': '(',
47+ // '}': '{',
48+ // ']': '['
49+ // }
50+
51+ // for (const ch of s) {
52+ // if (ch === '(' || ch === '{' || ch === '[') {
53+ // tempArray.push(ch);
54+ // } else {
55+ // if (tempArray.pop() !== pairObject[ch]) {
56+ // return false;
57+ // }
58+ // }
59+ // }
60+
61+ // return tempArray.length === 0;
62+ // };
63+
64+
65+
You can’t perform that action at this time.
0 commit comments