File tree Expand file tree Collapse file tree 1 file changed +95
-0
lines changed
Expand file tree Collapse file tree 1 file changed +95
-0
lines changed Original file line number Diff line number Diff line change 1+ # 1614.括号的最大嵌套深度
2+
3+ 如果字符串满足以下条件之一,则可以称之为 有效括号字符串(valid parentheses string,可以简写为 VPS):
4+
5+ 字符串是一个空字符串 "",或者是一个不为 "(" 或 ")" 的单字符。
6+ 字符串可以写为 AB(A 与 B 字符串连接),其中 A 和 B 都是 有效括号字符串 。
7+ 字符串可以写为 (A),其中 A 是一个 有效括号字符串 。
8+ 类似地,可以定义任何有效括号字符串 S 的 嵌套深度 depth(S):
9+
10+ depth("") = 0
11+ depth(C) = 0,其中 C 是单个字符的字符串,且该字符不是 "(" 或者 ")"
12+ depth(A + B) = max(depth(A), depth(B)),其中 A 和 B 都是 有效括号字符串
13+ depth("(" + A + ")") = 1 + depth(A),其中 A 是一个 有效括号字符串
14+ 例如:""、"()()"、"()(()())" 都是 有效括号字符串(嵌套深度分别为 0、1、2),而 ")(" 、"(()" 都不是 有效括号字符串 。
15+
16+ 给你一个 有效括号字符串 s,返回该字符串的 s 嵌套深度 。
17+
18+
19+
20+ 示例 1:
21+
22+ 输入:s = "(1+(2* 3)+((8)/4))+1"
23+ 输出:3
24+ 解释:数字 8 在嵌套的 3 层括号中。
25+ 示例 2:
26+
27+ 输入:s = "(1)+((2))+(((3)))"
28+ 输出:3
29+ 示例 3:
30+
31+ 输入:s = "1+(2* 3)/(2-1)"
32+ 输出:1
33+ 示例 4:
34+
35+ 输入:s = "1"
36+ 输出:0
37+
38+
39+ 提示:
40+
41+ 1 <= s.length <= 100
42+ s 由数字 0-9 和字符 '+'、'-'、'* '、'/'、'('、')' 组成
43+ 题目数据保证括号表达式 s 是 有效的括号表达式
44+
45+ 来源:力扣(LeetCode)
46+ 链接:https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses
47+ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
48+
49+
50+
51+ c语言
52+
53+ ``` c
54+ int maxDepth (char * s){
55+ int deep = 0;
56+ int size = 0;
57+ int n = strlen(s);
58+ for(int i = 0;i < n;i++){
59+ if(s[ i] =='('){
60+ size++;
61+
62+ }else if(s[ i] ==')'){
63+ size--;
64+ }c
65+ if(deep<=size){deep=size;}
66+ }
67+ return deep;
68+ }
69+ ```
70+
71+ java
72+
73+ 其中java使用.toCharArray()的方法将一个字符串转化为一个数组。
74+
75+ ```java
76+ class Solution {
77+ public int maxDepth(String s) {
78+ char [] stringArr =s.toCharArray();
79+ int deep = 0,size = 0;
80+ int n = s.length();
81+ for(int i = 0;i<n;i++){
82+ if(stringArr[i]=='('){
83+ size++;
84+ }else if(stringArr[i]==')'){
85+ size--;
86+ }
87+ if(size>=deep){
88+ deep=size;
89+ }
90+ }
91+ return deep;
92+ }
93+ }
94+ ```
95+
You can’t perform that action at this time.
0 commit comments