Leetcode 921 Javascript

pimkle發表於2019-01-09
/**
 * @param {string} S
 * @return {number}
 */
var minAddToMakeValid = function(S) {
    let stack = [];
    let len = S.length;
    for(let i = 0; i < len; ++i) {
        let popped = stack.pop();
        if(!popped) {
            stack.push(S[i]);
        } else if(!popped || popped !== '(' || S[i] !== ')') {
            stack.push(...[popped, S[i]])
        }
    }
    return stack.length;
};
複製程式碼

相關文章