Skip to content

Commit 64b9363

Browse files
committed
添加20. 有效的括号JavaScript版本
1 parent 0eb61a5 commit 64b9363

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

problems/0020.有效的括号.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ var isValid = function (s) {
271271
}
272272
return stack.length === 0;
273273
};
274+
// 简化版本
275+
var isValid = function(s) {
276+
const stack = [],
277+
map = {
278+
"(":")",
279+
"{":"}",
280+
"[":"]"
281+
};
282+
for(const x of s) {
283+
if(x in map) {
284+
stack.push(x);
285+
continue;
286+
};
287+
if(map[stack.pop()] !== x) return false;
288+
}
289+
return !stack.length;
290+
};
274291
```
275292

276293

0 commit comments

Comments
 (0)