Open LuckyWinty opened 4 years ago
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var stack = []
var map = {
'(' : ')',
'[': ']',
'{': '}'
}
for (var char of s) {
if(char in map) {
stack.push(char)
} else {
if( !stack.length || char != map[stack.pop()]) {
return false
}
}
}
// 如果最后stack 里没有元素了, 就一定是匹配的
return !stack.length
};
const judgeBracketsMatch = s=>{
let a = [...[...s.replace(/[^\(\)]/g,'')].reduce((m, k) => m.set(k, (m.get(k) || 0) + 1), new Map()).values()];
return a[0] === a[1]
}
const judgeBracketsMatch = s=>{ let a = [...[...s.replace(/[^\(\)]/g,'')].reduce((m, k) => m.set(k, (m.get(k) || 0) + 1), new Map()).values()]; return a[0] === a[1] }
简单跑了一下,你的是对的。你的markdown代码格式可以加上哦~比如
js 代码
+ const neOps = [')',']','}'];
+ if( neOps.includes(char) && ( !stack.length || char !== map[stack.pop()]))