Skip to content

Commit 374f4b6

Browse files
committed
feat: add solutions to lc problem: No.1021
No.1021.Remove Outermost Parentheses
1 parent 4908873 commit 374f4b6

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

solution/1000-1099/1021.Remove Outermost Parentheses/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,92 @@
7878
<!-- 这里可写当前语言的特殊实现逻辑 -->
7979

8080
```java
81+
class Solution {
82+
public String removeOuterParentheses(String S) {
83+
StringBuilder res = new StringBuilder();
84+
int cnt = 0;
85+
for (char c : S.toCharArray()) {
86+
if (c == '(') {
87+
if (++cnt > 1) {
88+
res.append('(');
89+
}
90+
} else {
91+
if (--cnt > 0) {
92+
res.append(')');
93+
}
94+
}
95+
}
96+
return res.toString();
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string removeOuterParentheses(string s) {
107+
string res;
108+
int depth = 0;
109+
for (char c: s) {
110+
if (c == '(') {
111+
depth++;
112+
}
113+
if (depth != 1) {
114+
res.push_back(c);
115+
}
116+
if (c == ')') {
117+
depth--;
118+
}
119+
}
120+
return res;
121+
}
122+
};
123+
```
124+
125+
### **TypeScript**
126+
127+
```ts
128+
function removeOuterParentheses(s: string): string {
129+
let res = '';
130+
let depth = 0;
131+
for (const c of s) {
132+
if (c === '(') {
133+
depth++;
134+
}
135+
if (depth !== 1) {
136+
res += c;
137+
}
138+
if (c === ')') {
139+
depth--;
140+
}
141+
}
142+
return res;
143+
}
144+
```
81145

146+
### **Rust**
147+
148+
```rust
149+
impl Solution {
150+
pub fn remove_outer_parentheses(s: String) -> String {
151+
let mut res = String::new();
152+
let mut depth = 0;
153+
for c in s.chars() {
154+
if c == '(' {
155+
depth += 1;
156+
}
157+
if depth != 1 {
158+
res.push(c);
159+
}
160+
if c == ')' {
161+
depth -= 1;
162+
}
163+
}
164+
res
165+
}
166+
}
82167
```
83168

84169
### **...**

solution/1000-1099/1021.Remove Outermost Parentheses/README_EN.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,92 @@ After removing outer parentheses of each part, this is &quot;&quot; + &quot;&quo
6969
### **Java**
7070

7171
```java
72+
class Solution {
73+
public String removeOuterParentheses(String S) {
74+
StringBuilder res = new StringBuilder();
75+
int cnt = 0;
76+
for (char c : S.toCharArray()) {
77+
if (c == '(') {
78+
if (++cnt > 1) {
79+
res.append('(');
80+
}
81+
} else {
82+
if (--cnt > 0) {
83+
res.append(')');
84+
}
85+
}
86+
}
87+
return res.toString();
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
string removeOuterParentheses(string s) {
98+
string res;
99+
int depth = 0;
100+
for (char c: s) {
101+
if (c == '(') {
102+
depth++;
103+
}
104+
if (depth != 1) {
105+
res.push_back(c);
106+
}
107+
if (c == ')') {
108+
depth--;
109+
}
110+
}
111+
return res;
112+
}
113+
};
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
function removeOuterParentheses(s: string): string {
120+
let res = '';
121+
let depth = 0;
122+
for (const c of s) {
123+
if (c === '(') {
124+
depth++;
125+
}
126+
if (depth !== 1) {
127+
res += c;
128+
}
129+
if (c === ')') {
130+
depth--;
131+
}
132+
}
133+
return res;
134+
}
135+
```
72136

137+
### **Rust**
138+
139+
```rust
140+
impl Solution {
141+
pub fn remove_outer_parentheses(s: String) -> String {
142+
let mut res = String::new();
143+
let mut depth = 0;
144+
for c in s.chars() {
145+
if c == '(' {
146+
depth += 1;
147+
}
148+
if depth != 1 {
149+
res.push(c);
150+
}
151+
if c == ')' {
152+
depth -= 1;
153+
}
154+
}
155+
res
156+
}
157+
}
73158
```
74159

75160
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
string removeOuterParentheses(string s) {
4+
string res;
5+
int depth = 0;
6+
for (char c: s) {
7+
if (c == '(') {
8+
depth++;
9+
}
10+
if (depth != 1) {
11+
res.push_back(c);
12+
}
13+
if (c == ')') {
14+
depth--;
15+
}
16+
}
17+
return res;
18+
}
19+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn remove_outer_parentheses(s: String) -> String {
3+
let mut res = String::new();
4+
let mut depth = 0;
5+
for c in s.chars() {
6+
if c == '(' {
7+
depth += 1;
8+
}
9+
if depth != 1 {
10+
res.push(c);
11+
}
12+
if c == ')' {
13+
depth -= 1;
14+
}
15+
}
16+
res
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function removeOuterParentheses(s: string): string {
2+
let res = '';
3+
let depth = 0;
4+
for (const c of s) {
5+
if (c === '(') {
6+
depth++;
7+
}
8+
if (depth !== 1) {
9+
res += c;
10+
}
11+
if (c === ')') {
12+
depth--;
13+
}
14+
}
15+
return res;
16+
}

0 commit comments

Comments
 (0)