添加“20.有效的括号”Javascript版本

This commit is contained in:
纪飞
2021-05-18 12:30:08 +08:00
parent 9fb6195c4c
commit a811e9aa69

View File

@ -247,6 +247,32 @@ def is_valid(strs)
end end
``` ```
Javascript:
```javascript
var isValid = function (s) {
const stack = [];
for (let i = 0; i < s.length; i++) {
let c = s[i];
switch (c) {
case '(':
stack.push(')');
break;
case '[':
stack.push(']');
break;
case '{':
stack.push('}');
break;
default:
if (c !== stack.pop()) {
return false;
}
}
}
return stack.length === 0;
};
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)