Skip to content

Commit 701dfd3

Browse files
Merge pull request #302 from SharingSource/ac_oier
✨feat: Add 1614
2 parents e72242a + c985ff0 commit 701dfd3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
| [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 |
8585
| [1576. 替换所有的问号](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-fa1u/) | 简单 | 🤩🤩🤩🤩🤩 |
8686
| [1583. 统计不开心的朋友](https://leetcode-cn.com/problems/count-unhappy-friends/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-unhappy-friends/solution/gong-shui-san-xie-ha-xi-biao-mo-ni-ti-by-2qy0/) | 中等 | 🤩🤩🤩🤩 |
87+
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
8788
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
8889
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
8990
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
如果字符串满足以下条件之一,则可以称之为 有效括号字符串(valid parentheses string,可以简写为 VPS):
10+
11+
* 字符串是一个空字符串 `""`,或者是一个不为 `"("``")"` 的单字符。
12+
* 字符串可以写为 `AB``A``B` 字符串连接),其中 `A``B` 都是 有效括号字符串 。
13+
* 字符串可以写为 `(A)`,其中 `A` 是一个 有效括号字符串 。
14+
15+
类似地,可以定义任何有效括号字符串 `S` 的 嵌套深度 `depth(S)`
16+
17+
* `depth("") = 0`
18+
* `depth(C) = 0`,其中 `C` 是单个字符的字符串,且该字符不是 `"("` 或者 `")"`
19+
* `depth(A + B) = max(depth(A), depth(B))`,其中 `A``B` 都是 有效括号字符串
20+
* `depth("(" + A + ")") = 1 + depth(A)`,其中 `A` 是一个 有效括号字符串
21+
22+
例如:`""``"()()"``"()(()())"` 都是 有效括号字符串(嵌套深度分别为 0、1、2),而 `")("``"(()"` 都不是 有效括号字符串 。
23+
24+
给你一个 有效括号字符串 `s`,返回该字符串的 `s` 嵌套深度 。
25+
26+
示例 1:
27+
```
28+
输入:s = "(1+(2*3)+((8)/4))+1"
29+
30+
输出:3
31+
32+
解释:数字 8 在嵌套的 3 层括号中。
33+
```
34+
示例 2:
35+
```
36+
输入:s = "(1)+((2))+(((3)))"
37+
38+
输出:3
39+
```
40+
示例 3:
41+
```
42+
输入:s = "1+(2*3)/(2-1)"
43+
输出:1
44+
```
45+
示例 4:
46+
```
47+
输入:s = "1"
48+
49+
输出:0
50+
```
51+
52+
提示:
53+
* $1 <= s.length <= 100$
54+
* `s` 由数字 `0-9` 和字符 `'+'``'-'``'*'``'/'``'('``')'` 组成
55+
* 题目数据保证括号表达式 `s` 是 有效的括号表达式
56+
57+
---
58+
59+
### 模拟
60+
61+
根据题意,其实就是求最大的连续左括号的数量(跳过普通字符,且与 `)` 抵消后),只需要边遍历边统计即可。
62+
63+
代码:
64+
```Java
65+
class Solution {
66+
public int maxDepth(String s) {
67+
int n = s.length(), ans = 0;
68+
for (int i = 0, cnt = 0; i < n; i++) {
69+
if (s.charAt(i) == '(') cnt++;
70+
else if (s.charAt(i) == ')') cnt--;
71+
ans = Math.max(ans, cnt);
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
* 时间复杂度:$O(n)$
78+
* 空间复杂度:$O(1)$
79+
80+
---
81+
82+
### 最后
83+
84+
这是我们「刷穿 LeetCode」系列文章的第 `No.1614` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
85+
86+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
87+
88+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
89+
90+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
91+

0 commit comments

Comments
 (0)