Skip to content

Commit 30501e4

Browse files
committed
feat: add solutions to lc problem: No.1614
No.1614.Maximum Nesting Depth of the Parentheses
1 parent e2bb7f6 commit 30501e4

File tree

8 files changed

+167
-57
lines changed

8 files changed

+167
-57
lines changed

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README.md

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@
8181
```python
8282
class Solution:
8383
def maxDepth(self, s: str) -> int:
84-
res = depth = 0
84+
n = ans = 0
8585
for c in s:
8686
if c == '(':
87-
depth += 1
88-
res = max(res, depth)
87+
n += 1
88+
ans = max(ans, n)
8989
elif c == ')':
90-
depth -= 1
91-
return res
90+
n -= 1
91+
return ans
9292
```
9393

9494
### **Java**
@@ -98,15 +98,15 @@ class Solution:
9898
```java
9999
class Solution {
100100
public int maxDepth(String s) {
101-
int res = 0, depth = 0;
101+
int n = 0, ans = 0;
102102
for (char c : s.toCharArray()) {
103103
if (c == '(') {
104-
res = Math.max(res, ++depth);
104+
ans = Math.max(ans, ++n);
105105
} else if (c == ')') {
106-
--depth;
106+
--n;
107107
}
108108
}
109-
return res;
109+
return ans;
110110
}
111111
}
112112
```
@@ -117,13 +117,13 @@ class Solution {
117117
class Solution {
118118
public:
119119
int maxDepth(string s) {
120-
int res = 0, depth =0;
120+
int n = 0, ans = 0;
121121
for (char c : s)
122122
{
123-
if (c == '(') res = max(res, ++depth);
124-
else if (c == ')') --depth;
123+
if (c == '(') ans = max(ans, ++n);
124+
else if (c == ')') --n;
125125
}
126-
return res;
126+
return ans;
127127
}
128128
};
129129
```
@@ -132,18 +132,58 @@ public:
132132
133133
```go
134134
func maxDepth(s string) int {
135-
res, depth := 0, 0
135+
n, ans := 0, 0
136136
for _, c := range s {
137137
if c == '(' {
138-
depth++
139-
if depth > res {
140-
res = depth
138+
n++
139+
if ans < n {
140+
ans = n
141141
}
142142
} else if c == ')' {
143-
depth--
143+
n--
144144
}
145145
}
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+
}
147187
}
148188
```
149189

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,30 @@
7373
```python
7474
class Solution:
7575
def maxDepth(self, s: str) -> int:
76-
res = depth = 0
76+
n = ans = 0
7777
for c in s:
7878
if c == '(':
79-
depth += 1
80-
res = max(res, depth)
79+
n += 1
80+
ans = max(ans, n)
8181
elif c == ')':
82-
depth -= 1
83-
return res
82+
n -= 1
83+
return ans
8484
```
8585

8686
### **Java**
8787

8888
```java
8989
class Solution {
9090
public int maxDepth(String s) {
91-
int res = 0, depth = 0;
91+
int n = 0, ans = 0;
9292
for (char c : s.toCharArray()) {
9393
if (c == '(') {
94-
res = Math.max(res, ++depth);
94+
ans = Math.max(ans, ++n);
9595
} else if (c == ')') {
96-
--depth;
96+
--n;
9797
}
9898
}
99-
return res;
99+
return ans;
100100
}
101101
}
102102
```
@@ -107,13 +107,13 @@ class Solution {
107107
class Solution {
108108
public:
109109
int maxDepth(string s) {
110-
int res = 0, depth =0;
110+
int n = 0, ans = 0;
111111
for (char c : s)
112112
{
113-
if (c == '(') res = max(res, ++depth);
114-
else if (c == ')') --depth;
113+
if (c == '(') ans = max(ans, ++n);
114+
else if (c == ')') --n;
115115
}
116-
return res;
116+
return ans;
117117
}
118118
};
119119
```
@@ -122,18 +122,58 @@ public:
122122
123123
```go
124124
func maxDepth(s string) int {
125-
res, depth := 0, 0
125+
n, ans := 0, 0
126126
for _, c := range s {
127127
if c == '(' {
128-
depth++
129-
if depth > res {
130-
res = depth
128+
n++
129+
if ans < n {
130+
ans = n
131131
}
132132
} else if c == ')' {
133-
depth--
133+
n--
134134
}
135135
}
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+
}
137177
}
138178
```
139179

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int maxDepth(string s) {
4-
int res = 0, depth =0;
4+
int n = 0, ans = 0;
55
for (char c : s)
66
{
7-
if (c == '(') res = max(res, ++depth);
8-
else if (c == ')') --depth;
7+
if (c == '(') ans = max(ans, ++n);
8+
else if (c == ')') --n;
99
}
10-
return res;
10+
return ans;
1111
}
1212
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func maxDepth(s string) int {
2-
res, depth := 0, 0
2+
n, ans := 0, 0
33
for _, c := range s {
44
if c == '(' {
5-
depth++
6-
if depth > res {
7-
res = depth
5+
n++
6+
if ans < n {
7+
ans = n
88
}
99
} else if c == ')' {
10-
depth--
10+
n--
1111
}
1212
}
13-
return res
13+
return ans
1414
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int maxDepth(String s) {
3-
int res = 0, depth = 0;
3+
int n = 0, ans = 0;
44
for (char c : s.toCharArray()) {
55
if (c == '(') {
6-
res = Math.max(res, ++depth);
6+
ans = Math.max(ans, ++n);
77
} else if (c == ')') {
8-
--depth;
8+
--n;
99
}
1010
}
11-
return res;
11+
return ans;
1212
}
1313
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
def maxDepth(self, s: str) -> int:
3-
res = depth = 0
3+
n = ans = 0
44
for c in s:
55
if c == '(':
6-
depth += 1
7-
res = max(res, depth)
6+
n += 1
7+
ans = max(ans, n)
88
elif c == ')':
9-
depth -= 1
10-
return res
9+
n -= 1
10+
return ans

0 commit comments

Comments
 (0)