【Leetcode】【240404】1614. Maximum Nesting Depth of the Parentheses

BGM(?):圣堂之门-阿沁《梵谷的左耳》

Description

A string is a valid parentheses string (denoted VPS) if it meets one of the following:

It is an empty string “”, or a single character not equal to “(” or “)”,
It can be written as AB (A concatenated with B), where A and B are VPS’s, or
It can be written as (A), where A is a VPS.
We can similarly define the nesting depth depth(S) of any VPS S as follows:

depth(“”) = 0
depth© = 0, where C is a string with a single character not equal to “(” or “)”.
depth(A + B) = max(depth(A), depth(B)), where A and B are VPS’s.
depth(“(” + A + “)”) = 1 + depth(A), where A is a VPS.
For example, “”, “()()”, and “()(()())” are VPS’s (with nesting depths 0, 1, and 2), and “)(” and “(()” are not VPS’s.

Given a VPS represented as string s, return the nesting depth of s.

Example 1:

Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3

Explanation: Digit 8 is inside of 3 nested parentheses in the string.

##Example 2:

Input: s = "(1)+((2))+(((3)))"
Output: 3

Constraints:

1 <= s.length <= 100
s consists of digits 0-9 and characters ‘+’, ‘-’, ‘*’, ‘/’, ‘(’, and ‘)’.
It is guaranteed that parentheses expression s is a VPS.

Solution

Try to think it easy: define s for the number of (, and deep for the deep

  • When visit’(', just count start.
  • When visit’)', regarding as a VPS finish, compare current deep with start. Finally, sub the start as the ‘layer’ of this nest has been finish

Code

class Solution {
public:
    int maxDepth(string s)
    {
        if(s==" "||(s.length()==1&&s!="("&&s!=")")) return 0;
        else
        {
            int deep=0,start=0;
            for(int i=0;i<s.size();++i)
            {
                if(s[i]=='(')
                {
                    ++start;
                }
                else if(s[i]==')') 
                {
                    deep=max(deep,start);
                    --start;
                }
            }
            return deep;
        }
        return 0;
    }
};

相关推荐

  1. leetcode

    2024-04-05 02:22:03       57 阅读
  2. leetcode

    2024-04-05 02:22:03       57 阅读
  3. leetcode

    2024-04-05 02:22:03       65 阅读
  4. LeetCode

    2024-04-05 02:22:03       36 阅读
  5. leetcode

    2024-04-05 02:22:03       32 阅读
  6. Leetcode -2

    2024-04-05 02:22:03       52 阅读
  7. Leetcode】计算器

    2024-04-05 02:22:03       65 阅读
  8. LeetCode 45

    2024-04-05 02:22:03       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 02:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 02:22:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 02:22:03       82 阅读
  4. Python语言-面向对象

    2024-04-05 02:22:03       91 阅读

热门阅读

  1. 零日攻击

    2024-04-05 02:22:03       41 阅读
  2. 01背包问题

    2024-04-05 02:22:03       41 阅读
  3. 从Jenkinsfile构建到k8s部署

    2024-04-05 02:22:03       44 阅读
  4. Git/GitHub/Gitee⼯作流最佳实践

    2024-04-05 02:22:03       42 阅读
  5. 机器学习——典型的卷积神经网络

    2024-04-05 02:22:03       37 阅读
  6. 第十三题:天干地支

    2024-04-05 02:22:03       32 阅读
  7. 什么叫地下水年龄?

    2024-04-05 02:22:03       37 阅读
  8. IO流c++

    IO流c++

    2024-04-05 02:22:03      37 阅读
  9. 关于在PyCharm中使用虚拟环境

    2024-04-05 02:22:03       39 阅读