/** * * @param s string字符串 * @return bool布尔型 */ function isValid( s ) { // write code here const truely = ["()","[]","{}"] function check() { const checkList = truely.filter( item => { return s.indexOf( item ) > -1 }) return checkList.length } while( check() > 0 ) { truely.forEach( item => { s = s.replace( item, "" ) }) } if( s.length > 0 ) { return false }else { return true } } module.exports = { isValid : isValid };
看过了各位大佬的思路,基本都是用“栈”的思想,我这里算是高级语言的一个思路。
1、无论哪种括号,在最里面一层一定是成对出现的;
2、将所有成对括号删除剩下的仍然是成对括号;
3、若最后有剩余则不满足条件;