1021.删除最外层的括号

时游小于 1 分钟LeetCode

1021.删除最外层的括号

/*
 * @lc app=leetcode.cn id=1021 lang=typescript
 *
 * [1021] 删除最外层的括号
 */

// @lc code=start
function removeOuterParentheses(s: string): string {
	let stack = [];
	let final: string[] = [];
	let l = 0,
		r = 0;
	while (r < s.length) {
		if (s[r] == "(") {
			stack.push(1);
		} else {
			stack.pop();
		}

		if (stack.length == 0) {
			// 结束
			final.push(s.slice(l + 1, r));
			l = r + 1;
		}
		r++;
	}

	console.log(final);

	return final.join("");
}

console.log(removeOuterParentheses("(()())(())"));

// @lc code=end

上次编辑于:
贡献者: 15327360835
Loading...