Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26d9b95

Browse files
✨feat: Add 1021
1 parent 5512c15 commit 26d9b95

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩 |
3838
| [992. K 个不同整数的子数组](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/solution/miao-dong-xi-lie-xiang-jie-shuang-zhi-zh-9k8w/) | 困难 | 🤩🤩🤩🤩 |
3939
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
40+
| [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/) | 简单 | 🤩🤩🤩🤩 |
4041
| [1052. 爱生气的书店老板](https://leetcode-cn.com/problems/grumpy-bookstore-owner/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grumpy-bookstore-owner/solution/hua-dong-chuang-kou-luo-ti-by-ac_oier-nunu/) | 中等 | 🤩🤩🤩 |
4142
| [1221. 分割平衡字符串](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-wumnk/) | 简单 | 🤩🤩🤩🤩 |
4243
| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 简单 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
111111
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
112112
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
113+
| [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/) | 简单 | 🤩🤩🤩🤩 |
113114
| [1047. 删除字符串中的所有相邻重复项](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/solution/cong-30-dao-100wu-chong-shi-xian-jie-jue-vkah/) | 简单 | 🤩🤩🤩🤩 |
114115
| [1078. Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/occurrences-after-bigram/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-qyki/) | 简单 | 🤩🤩🤩🤩 |
115116
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「双指针」
6+
7+
8+
9+
有效括号字符串为空 `""``"(" + A + ")"``A + B` ,其中 `A``B` 都是有效的括号字符串,`+` 代表字符串的连接。
10+
11+
例如,`""`,`"()"`,`"(())()"``"(()(()))"` 都是有效的括号字符串。
12+
13+
如果有效字符串 `s` 非空,且不存在将其拆分为 `s = A + B` 的方法,我们称其为原语(`primitive`),其中 `A``B` 都是非空有效括号字符串。
14+
15+
给出一个非空有效字符串 `s`,考虑将其进行原语化分解,使得:`s = P_1 + P_2 + ... + P_k`,其中 `P_i` 是有效括号字符串原语。
16+
17+
`s` 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 s 。
18+
19+
示例 1:
20+
```
21+
输入:s = "(()())(())"
22+
23+
输出:"()()()"
24+
25+
解释:
26+
输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
27+
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。
28+
```
29+
示例 2:
30+
```
31+
输入:s = "(()())(())(()(()))"
32+
33+
输出:"()()()()(())"
34+
35+
解释:
36+
输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
37+
删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。
38+
```
39+
示例 3:
40+
```
41+
输入:s = "()()"
42+
43+
输出:""
44+
45+
解释:
46+
输入字符串为 "()()",原语化分解得到 "()" + "()",
47+
删除每个部分中的最外层括号后得到 "" + "" = ""。
48+
```
49+
50+
提示:
51+
* 1ドル <= s.length <= 10^5$
52+
* `s[i]``'('``')'`
53+
* `s` 是一个有效括号字符串
54+
55+
---
56+
57+
### 双指针模拟
58+
59+
和其他「有效括号」问题一样,我们将 `(` 记为 1ドル$ 分,将 `)` 记为 $-1$。
60+
61+
由于 `s` 本身为有效字符串,因此我们可以从前往后进行处理,每一次找到符合 $socre = 0$ 的连续段 $[i, j),ドル然后再结合题意,删掉外层括号,即将 $[i + 1, j - 1)$ 加入答案。
62+
63+
代码:
64+
```Java
65+
class Solution {
66+
public String removeOuterParentheses(String s) {
67+
char[] cs = s.toCharArray();
68+
int n = cs.length;
69+
StringBuilder sb = new StringBuilder();
70+
for (int i = 0, score = 0; i < n; ) {
71+
int j = i;
72+
while (i == j || score != 0) score += cs[j++] == '(' ? 1 : -1;
73+
sb.append(s.substring(i + 1, j - 1));
74+
i = j;
75+
score = 0;
76+
}
77+
return sb.toString();
78+
}
79+
}
80+
```
81+
* 时间复杂度:$O(n)$
82+
* 空间复杂度:$O(1)$
83+
84+
---
85+
86+
### 最后
87+
88+
这是我们「刷穿 LeetCode」系列文章的第 `No.1021` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
89+
90+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
91+
92+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
93+
94+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
95+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /