Skip to content

Commit fdb3e05

Browse files
committed
feat: update solutions to lc problems: No.1614,1616
* No.1614.Maximum Nesting Depth of the Parentheses * No.1616.Split Two Strings to Make Palindrome
1 parent 93ccac6 commit fdb3e05

File tree

12 files changed

+194
-133
lines changed

12 files changed

+194
-133
lines changed

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

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:遍历**
62+
63+
我们可以遍历字符串,维护当前的嵌套深度,遇到左括号时深度加一,并且更新组最大深大;遇到右括号时深度减一。
64+
65+
遍历结束后,返回最大深度即可。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
@@ -67,13 +75,13 @@
6775
```python
6876
class Solution:
6977
def maxDepth(self, s: str) -> int:
70-
n = ans = 0
78+
ans = d = 0
7179
for c in s:
7280
if c == '(':
73-
n += 1
74-
ans = max(ans, n)
81+
d += 1
82+
ans = max(ans, d)
7583
elif c == ')':
76-
n -= 1
84+
d -= 1
7785
return ans
7886
```
7987

@@ -84,12 +92,13 @@ class Solution:
8492
```java
8593
class Solution {
8694
public int maxDepth(String s) {
87-
int n = 0, ans = 0;
88-
for (char c : s.toCharArray()) {
95+
int ans = 0, d = 0;
96+
for (int i = 0; i < s.length(); ++i) {
97+
char c = s.charAt(i);
8998
if (c == '(') {
90-
ans = Math.max(ans, ++n);
99+
ans = Math.max(ans, ++d);
91100
} else if (c == ')') {
92-
--n;
101+
--d;
93102
}
94103
}
95104
return ans;
@@ -103,12 +112,13 @@ class Solution {
103112
class Solution {
104113
public:
105114
int maxDepth(string s) {
106-
int n = 0, ans = 0;
107-
for (char c : s) {
108-
if (c == '(')
109-
ans = max(ans, ++n);
110-
else if (c == ')')
111-
--n;
115+
int ans = 0, d = 0;
116+
for (char& c : s) {
117+
if (c == '(') {
118+
ans = max(ans, ++d);
119+
} else if (c == ')') {
120+
--d;
121+
}
112122
}
113123
return ans;
114124
}
@@ -118,19 +128,24 @@ public:
118128
### **Go**
119129
120130
```go
121-
func maxDepth(s string) int {
122-
n, ans := 0, 0
131+
func maxDepth(s string) (ans int) {
132+
d := 0
123133
for _, c := range s {
124134
if c == '(' {
125-
n++
126-
if ans < n {
127-
ans = n
128-
}
135+
d++
136+
ans = max(ans, d)
129137
} else if c == ')' {
130-
n--
138+
d--
131139
}
132140
}
133-
return ans
141+
return
142+
}
143+
144+
func max(a, b int) int {
145+
if a > b {
146+
return a
147+
}
148+
return b
134149
}
135150
```
136151

@@ -142,11 +157,14 @@ func maxDepth(s string) int {
142157
* @return {number}
143158
*/
144159
var maxDepth = function (s) {
145-
let n = 0,
146-
ans = 0;
147-
for (let c of s) {
148-
if (c == '(') ans = Math.max(ans, ++n);
149-
else if (c == ')') --n;
160+
let ans = 0;
161+
let d = 0;
162+
for (const c of s) {
163+
if (c === '(') {
164+
ans = Math.max(ans, ++d);
165+
} else if (c === ')') {
166+
--d;
167+
}
150168
}
151169
return ans;
152170
};
@@ -157,23 +175,36 @@ var maxDepth = function (s) {
157175
```cs
158176
public class Solution {
159177
public int MaxDepth(string s) {
160-
int n = 0, ans = 0;
161-
foreach (char c in s)
162-
{
163-
if (c == '(')
164-
{
165-
ans = Math.Max(ans, ++n);
166-
}
167-
else if (c == ')')
168-
{
169-
--n;
178+
int ans = 0, d = 0;
179+
foreach(char c in s) {
180+
if (c == '(') {
181+
ans = Math.Max(ans, ++d);
182+
} else if (c == ')') {
183+
--d;
170184
}
171185
}
172186
return ans;
173187
}
174188
}
175189
```
176190

191+
### **TypeScript**
192+
193+
```ts
194+
function maxDepth(s: string): number {
195+
let ans = 0;
196+
let d = 0;
197+
for (const c of s) {
198+
if (c === '(') {
199+
ans = Math.max(ans, ++d);
200+
} else if (c === ')') {
201+
--d;
202+
}
203+
}
204+
return ans;
205+
}
206+
```
207+
177208
### **...**
178209

179210
```

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

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
```python
6060
class Solution:
6161
def maxDepth(self, s: str) -> int:
62-
n = ans = 0
62+
ans = d = 0
6363
for c in s:
6464
if c == '(':
65-
n += 1
66-
ans = max(ans, n)
65+
d += 1
66+
ans = max(ans, d)
6767
elif c == ')':
68-
n -= 1
68+
d -= 1
6969
return ans
7070
```
7171

@@ -74,12 +74,13 @@ class Solution:
7474
```java
7575
class Solution {
7676
public int maxDepth(String s) {
77-
int n = 0, ans = 0;
78-
for (char c : s.toCharArray()) {
77+
int ans = 0, d = 0;
78+
for (int i = 0; i < s.length(); ++i) {
79+
char c = s.charAt(i);
7980
if (c == '(') {
80-
ans = Math.max(ans, ++n);
81+
ans = Math.max(ans, ++d);
8182
} else if (c == ')') {
82-
--n;
83+
--d;
8384
}
8485
}
8586
return ans;
@@ -93,12 +94,13 @@ class Solution {
9394
class Solution {
9495
public:
9596
int maxDepth(string s) {
96-
int n = 0, ans = 0;
97-
for (char c : s) {
98-
if (c == '(')
99-
ans = max(ans, ++n);
100-
else if (c == ')')
101-
--n;
97+
int ans = 0, d = 0;
98+
for (char& c : s) {
99+
if (c == '(') {
100+
ans = max(ans, ++d);
101+
} else if (c == ')') {
102+
--d;
103+
}
102104
}
103105
return ans;
104106
}
@@ -108,19 +110,24 @@ public:
108110
### **Go**
109111
110112
```go
111-
func maxDepth(s string) int {
112-
n, ans := 0, 0
113+
func maxDepth(s string) (ans int) {
114+
d := 0
113115
for _, c := range s {
114116
if c == '(' {
115-
n++
116-
if ans < n {
117-
ans = n
118-
}
117+
d++
118+
ans = max(ans, d)
119119
} else if c == ')' {
120-
n--
120+
d--
121121
}
122122
}
123-
return ans
123+
return
124+
}
125+
126+
func max(a, b int) int {
127+
if a > b {
128+
return a
129+
}
130+
return b
124131
}
125132
```
126133

@@ -132,11 +139,14 @@ func maxDepth(s string) int {
132139
* @return {number}
133140
*/
134141
var maxDepth = function (s) {
135-
let n = 0,
136-
ans = 0;
137-
for (let c of s) {
138-
if (c == '(') ans = Math.max(ans, ++n);
139-
else if (c == ')') --n;
142+
let ans = 0;
143+
let d = 0;
144+
for (const c of s) {
145+
if (c === '(') {
146+
ans = Math.max(ans, ++d);
147+
} else if (c === ')') {
148+
--d;
149+
}
140150
}
141151
return ans;
142152
};
@@ -147,23 +157,36 @@ var maxDepth = function (s) {
147157
```cs
148158
public class Solution {
149159
public int MaxDepth(string s) {
150-
int n = 0, ans = 0;
151-
foreach (char c in s)
152-
{
153-
if (c == '(')
154-
{
155-
ans = Math.Max(ans, ++n);
156-
}
157-
else if (c == ')')
158-
{
159-
--n;
160+
int ans = 0, d = 0;
161+
foreach(char c in s) {
162+
if (c == '(') {
163+
ans = Math.Max(ans, ++d);
164+
} else if (c == ')') {
165+
--d;
160166
}
161167
}
162168
return ans;
163169
}
164170
}
165171
```
166172

173+
### **TypeScript**
174+
175+
```ts
176+
function maxDepth(s: string): number {
177+
let ans = 0;
178+
let d = 0;
179+
for (const c of s) {
180+
if (c === '(') {
181+
ans = Math.max(ans, ++d);
182+
} else if (c === ')') {
183+
--d;
184+
}
185+
}
186+
return ans;
187+
}
188+
```
189+
167190
### **...**
168191

169192
```

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
33
int maxDepth(string s) {
4-
int n = 0, ans = 0;
5-
for (char c : s) {
6-
if (c == '(')
7-
ans = max(ans, ++n);
8-
else if (c == ')')
9-
--n;
4+
int ans = 0, d = 0;
5+
for (char& c : s) {
6+
if (c == '(') {
7+
ans = max(ans, ++d);
8+
} else if (c == ')') {
9+
--d;
10+
}
1011
}
1112
return ans;
1213
}

solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
public class Solution {
22
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;
3+
int ans = 0, d = 0;
4+
foreach(char c in s) {
5+
if (c == '(') {
6+
ans = Math.Max(ans, ++d);
7+
} else if (c == ')') {
8+
--d;
139
}
1410
}
1511
return ans;
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
func maxDepth(s string) int {
2-
n, ans := 0, 0
1+
func maxDepth(s string) (ans int) {
2+
d := 0
33
for _, c := range s {
44
if c == '(' {
5-
n++
6-
if ans < n {
7-
ans = n
8-
}
5+
d++
6+
ans = max(ans, d)
97
} else if c == ')' {
10-
n--
8+
d--
119
}
1210
}
13-
return ans
11+
return
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
1419
}

0 commit comments

Comments
 (0)