File tree Expand file tree Collapse file tree 8 files changed +167
-57
lines changed
solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses Expand file tree Collapse file tree 8 files changed +167
-57
lines changed Original file line number Diff line number Diff line change 81
81
``` python
82
82
class Solution :
83
83
def maxDepth (self , s : str ) -> int :
84
- res = depth = 0
84
+ n = ans = 0
85
85
for c in s:
86
86
if c == ' (' :
87
- depth += 1
88
- res = max (res, depth )
87
+ n += 1
88
+ ans = max (ans, n )
89
89
elif c == ' )' :
90
- depth -= 1
91
- return res
90
+ n -= 1
91
+ return ans
92
92
```
93
93
94
94
### ** Java**
@@ -98,15 +98,15 @@ class Solution:
98
98
``` java
99
99
class Solution {
100
100
public int maxDepth (String s ) {
101
- int res = 0 , depth = 0 ;
101
+ int n = 0 , ans = 0 ;
102
102
for (char c : s. toCharArray()) {
103
103
if (c == ' (' ) {
104
- res = Math . max(res , ++ depth );
104
+ ans = Math . max(ans , ++ n );
105
105
} else if (c == ' )' ) {
106
- -- depth ;
106
+ -- n ;
107
107
}
108
108
}
109
- return res ;
109
+ return ans ;
110
110
}
111
111
}
112
112
```
@@ -117,13 +117,13 @@ class Solution {
117
117
class Solution {
118
118
public:
119
119
int maxDepth(string s) {
120
- int res = 0, depth = 0;
120
+ int n = 0, ans = 0;
121
121
for (char c : s)
122
122
{
123
- if (c == '(') res = max(res , ++depth );
124
- else if (c == ')') --depth ;
123
+ if (c == '(') ans = max(ans , ++n );
124
+ else if (c == ')') --n ;
125
125
}
126
- return res ;
126
+ return ans ;
127
127
}
128
128
};
129
129
```
@@ -132,18 +132,58 @@ public:
132
132
133
133
```go
134
134
func maxDepth(s string) int {
135
- res, depth := 0, 0
135
+ n, ans := 0, 0
136
136
for _, c := range s {
137
137
if c == '(' {
138
- depth ++
139
- if depth > res {
140
- res = depth
138
+ n ++
139
+ if ans < n {
140
+ ans = n
141
141
}
142
142
} else if c == ')' {
143
- depth --
143
+ n --
144
144
}
145
145
}
146
- return res
146
+ return ans
147
+ }
148
+ ```
149
+
150
+ ### ** JavaScript**
151
+
152
+ ``` js
153
+ /**
154
+ * @param {string} s
155
+ * @return {number}
156
+ */
157
+ var maxDepth = function (s ) {
158
+ let n = 0 ,
159
+ ans = 0 ;
160
+ for (let c of s) {
161
+ if (c == ' (' ) ans = Math .max (ans, ++ n);
162
+ else if (c == ' )' ) -- n;
163
+ }
164
+ return ans;
165
+ };
166
+ ```
167
+
168
+ ### ** C#**
169
+
170
+ ``` cs
171
+ public class Solution {
172
+ public int MaxDepth (string s ) {
173
+ int n = 0 , ans = 0 ;
174
+ foreach (char c in s )
175
+ {
176
+ if (c == '(' )
177
+ {
178
+ ans = Math .Max (ans , ++ n );
179
+ }
180
+ else if (c == ')' )
181
+ {
182
+ -- n ;
183
+ }
184
+ }
185
+ return ans ;
186
+ }
147
187
}
148
188
```
149
189
Original file line number Diff line number Diff line change 73
73
``` python
74
74
class Solution :
75
75
def maxDepth (self , s : str ) -> int :
76
- res = depth = 0
76
+ n = ans = 0
77
77
for c in s:
78
78
if c == ' (' :
79
- depth += 1
80
- res = max (res, depth )
79
+ n += 1
80
+ ans = max (ans, n )
81
81
elif c == ' )' :
82
- depth -= 1
83
- return res
82
+ n -= 1
83
+ return ans
84
84
```
85
85
86
86
### ** Java**
87
87
88
88
``` java
89
89
class Solution {
90
90
public int maxDepth (String s ) {
91
- int res = 0 , depth = 0 ;
91
+ int n = 0 , ans = 0 ;
92
92
for (char c : s. toCharArray()) {
93
93
if (c == ' (' ) {
94
- res = Math . max(res , ++ depth );
94
+ ans = Math . max(ans , ++ n );
95
95
} else if (c == ' )' ) {
96
- -- depth ;
96
+ -- n ;
97
97
}
98
98
}
99
- return res ;
99
+ return ans ;
100
100
}
101
101
}
102
102
```
@@ -107,13 +107,13 @@ class Solution {
107
107
class Solution {
108
108
public:
109
109
int maxDepth(string s) {
110
- int res = 0, depth = 0;
110
+ int n = 0, ans = 0;
111
111
for (char c : s)
112
112
{
113
- if (c == '(') res = max(res , ++depth );
114
- else if (c == ')') --depth ;
113
+ if (c == '(') ans = max(ans , ++n );
114
+ else if (c == ')') --n ;
115
115
}
116
- return res ;
116
+ return ans ;
117
117
}
118
118
};
119
119
```
@@ -122,18 +122,58 @@ public:
122
122
123
123
```go
124
124
func maxDepth(s string) int {
125
- res, depth := 0, 0
125
+ n, ans := 0, 0
126
126
for _, c := range s {
127
127
if c == '(' {
128
- depth ++
129
- if depth > res {
130
- res = depth
128
+ n ++
129
+ if ans < n {
130
+ ans = n
131
131
}
132
132
} else if c == ')' {
133
- depth --
133
+ n --
134
134
}
135
135
}
136
- return res
136
+ return ans
137
+ }
138
+ ```
139
+
140
+ ### ** JavaScript**
141
+
142
+ ``` js
143
+ /**
144
+ * @param {string} s
145
+ * @return {number}
146
+ */
147
+ var maxDepth = function (s ) {
148
+ let n = 0 ,
149
+ ans = 0 ;
150
+ for (let c of s) {
151
+ if (c == ' (' ) ans = Math .max (ans, ++ n);
152
+ else if (c == ' )' ) -- n;
153
+ }
154
+ return ans;
155
+ };
156
+ ```
157
+
158
+ ### ** C#**
159
+
160
+ ``` cs
161
+ public class Solution {
162
+ public int MaxDepth (string s ) {
163
+ int n = 0 , ans = 0 ;
164
+ foreach (char c in s )
165
+ {
166
+ if (c == '(' )
167
+ {
168
+ ans = Math .Max (ans , ++ n );
169
+ }
170
+ else if (c == ')' )
171
+ {
172
+ -- n ;
173
+ }
174
+ }
175
+ return ans ;
176
+ }
137
177
}
138
178
```
139
179
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
int maxDepth (string s) {
4
- int res = 0 , depth = 0 ;
4
+ int n = 0 , ans = 0 ;
5
5
for (char c : s)
6
6
{
7
- if (c == ' (' ) res = max (res , ++depth );
8
- else if (c == ' )' ) --depth ;
7
+ if (c == ' (' ) ans = max (ans , ++n );
8
+ else if (c == ' )' ) --n ;
9
9
}
10
- return res ;
10
+ return ans ;
11
11
}
12
12
};
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int MaxDepth ( string s ) {
3
+ int n = 0 , ans = 0 ;
4
+ foreach ( char c in s )
5
+ {
6
+ if ( c == '(' )
7
+ {
8
+ ans = Math . Max ( ans , ++ n ) ;
9
+ }
10
+ else if ( c == ')' )
11
+ {
12
+ -- n ;
13
+ }
14
+ }
15
+ return ans ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change 1
1
func maxDepth (s string ) int {
2
- res , depth := 0 , 0
2
+ n , ans := 0 , 0
3
3
for _ , c := range s {
4
4
if c == '(' {
5
- depth ++
6
- if depth > res {
7
- res = depth
5
+ n ++
6
+ if ans < n {
7
+ ans = n
8
8
}
9
9
} else if c == ')' {
10
- depth --
10
+ n --
11
11
}
12
12
}
13
- return res
13
+ return ans
14
14
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maxDepth (String s ) {
3
- int res = 0 , depth = 0 ;
3
+ int n = 0 , ans = 0 ;
4
4
for (char c : s .toCharArray ()) {
5
5
if (c == '(' ) {
6
- res = Math .max (res , ++depth );
6
+ ans = Math .max (ans , ++n );
7
7
} else if (c == ')' ) {
8
- --depth ;
8
+ --n ;
9
9
}
10
10
}
11
- return res ;
11
+ return ans ;
12
12
}
13
13
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var maxDepth = function ( s ) {
6
+ let n = 0 ,
7
+ ans = 0 ;
8
+ for ( let c of s ) {
9
+ if ( c == '(' ) ans = Math . max ( ans , ++ n ) ;
10
+ else if ( c == ')' ) -- n ;
11
+ }
12
+ return ans ;
13
+ } ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maxDepth (self , s : str ) -> int :
3
- res = depth = 0
3
+ n = ans = 0
4
4
for c in s :
5
5
if c == '(' :
6
- depth += 1
7
- res = max (res , depth )
6
+ n += 1
7
+ ans = max (ans , n )
8
8
elif c == ')' :
9
- depth -= 1
10
- return res
9
+ n -= 1
10
+ return ans
You can’t perform that action at this time.
0 commit comments