如果字符串满足以下条件之一,则可以称之为 有效括号字符串 (valid parentheses string,可以简写为 VPS):
-
字符串是一个空字符串
"",或者是一个不为"("或")"的单字符。 -
字符串可以写为
AB(A与B字符串连接),其中A和B都是 有效括号字符串 。 -
字符串可以写为
(A),其中A是一个 有效括号字符串 。
类似地,可以定义任何有效括号字符串 S 的 嵌套深度 depth(S):
-
depth("") = 0 -
depth(C) = 0,其中C是单个字符的字符串,且该字符不是"("或者")" -
depth(A + B) = max(depth(A), depth(B)),其中A和B都是 有效括号字符串 -
depth("(" + A + ")") = 1 + depth(A),其中A是一个 有效括号字符串
例如:""、"()()"、"()(()())" 都是 有效括号字符串(嵌套深度分别为 0、1、2),而 ")(" 、"(()" 都不是 有效括号字符串 。
给你一个 有效括号字符串 s,返回该字符串的 **s 嵌套深度 。
示例 1:
输入: s = "(1+(2*3)+((8)/4))+1"
输出: 3
解释: 数字 8 在嵌套的 3 层括号中。
示例 2:
输入: s = "(1)+((2))+(((3)))"
输出: 3
题解:
/**
* @param {string} s
* @return {number}
*/
// 方法一:用栈存储
var maxDepth = function (s) {
const stack = [];
let res = 0
for (let ca of s) {
if (ca == '(') {
stack.push('(')
res = Math.max(res, stack.length)
} else if (ca == ')') {
stack.pop()
}
}
return res
};
// 方法二:计数
var maxDepth = function (s) {
let size = 0;
let res = 0
for (let ca of s) {
if (ca == '(') {
size++
res = Math.max(res, size)
} else if (ca == ')') {
size--
}
}
return res
};
来源:力扣(LeetCode)
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。