Skip to content

Commit f5166d3

Browse files
committed
feat: add solutions to lc problem: No.0921
No.0921.Minimum Add to Make Parentheses Valid
1 parent 3bd9bd7 commit f5166d3

File tree

8 files changed

+206
-4
lines changed

8 files changed

+206
-4
lines changed

solution/0000-0099/0078.Subsets/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func subsets(nums []int) [][]int {
140140
}
141141
```
142142

143-
### **Rust1**
143+
### **Rust**
144144

145145
```rust
146146
impl Solution {

solution/0000-0099/0078.Subsets/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func subsets(nums []int) [][]int {
132132
}
133133
```
134134

135-
### **Rust1**
135+
### **Rust**
136136

137137
```rust
138138
impl Solution {

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,85 @@
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class Solution:
62+
def minAddToMakeValid(self, s: str) -> int:
63+
stk = []
64+
for c in s:
65+
if c == '(':
66+
stk.append(c)
67+
else:
68+
if stk and stk[-1] == '(':
69+
stk.pop()
70+
else:
71+
stk.append(c)
72+
return len(stk)
6273
```
6374

6475
### **Java**
6576

6677
<!-- 这里可写当前语言的特殊实现逻辑 -->
6778

6879
```java
80+
class Solution {
81+
public int minAddToMakeValid(String s) {
82+
Deque<Character> stk = new ArrayDeque<>();
83+
for (char c : s.toCharArray()) {
84+
if (c == '(') {
85+
stk.push(c);
86+
} else {
87+
if (!stk.isEmpty() && stk.peek() == '(') {
88+
stk.pop();
89+
} else {
90+
stk.push(c);
91+
}
92+
}
93+
}
94+
return stk.size();
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int minAddToMakeValid(string s) {
105+
stack<char> stk;
106+
for (char& c: s)
107+
{
108+
if (c == '(') stk.push(c);
109+
else
110+
{
111+
if (!stk.empty() && stk.top() == '(') {
112+
stk.pop();
113+
}
114+
else stk.push(c);
115+
}
116+
}
117+
return stk.size();
118+
}
119+
};
120+
```
69121
122+
### **Go**
123+
124+
```go
125+
func minAddToMakeValid(s string) int {
126+
var stk []rune
127+
for _, c := range s {
128+
if c == '(' {
129+
stk = append(stk, c)
130+
} else {
131+
if len(stk) > 0 && stk[len(stk)-1] == '(' {
132+
stk = stk[:len(stk)-1]
133+
} else {
134+
stk = append(stk, c)
135+
}
136+
}
137+
}
138+
return len(stk)
139+
}
70140
```
71141

72142
### **...**

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,83 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def minAddToMakeValid(self, s: str) -> int:
55+
stk = []
56+
for c in s:
57+
if c == '(':
58+
stk.append(c)
59+
else:
60+
if stk and stk[-1] == '(':
61+
stk.pop()
62+
else:
63+
stk.append(c)
64+
return len(stk)
5465
```
5566

5667
### **Java**
5768

5869
```java
70+
class Solution {
71+
public int minAddToMakeValid(String s) {
72+
Deque<Character> stk = new ArrayDeque<>();
73+
for (char c : s.toCharArray()) {
74+
if (c == '(') {
75+
stk.push(c);
76+
} else {
77+
if (!stk.isEmpty() && stk.peek() == '(') {
78+
stk.pop();
79+
} else {
80+
stk.push(c);
81+
}
82+
}
83+
}
84+
return stk.size();
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int minAddToMakeValid(string s) {
95+
stack<char> stk;
96+
for (char& c: s)
97+
{
98+
if (c == '(') stk.push(c);
99+
else
100+
{
101+
if (!stk.empty() && stk.top() == '(') {
102+
stk.pop();
103+
}
104+
else stk.push(c);
105+
}
106+
}
107+
return stk.size();
108+
}
109+
};
110+
```
59111
112+
### **Go**
113+
114+
```go
115+
func minAddToMakeValid(s string) int {
116+
var stk []rune
117+
for _, c := range s {
118+
if c == '(' {
119+
stk = append(stk, c)
120+
} else {
121+
if len(stk) > 0 && stk[len(stk)-1] == '(' {
122+
stk = stk[:len(stk)-1]
123+
} else {
124+
stk = append(stk, c)
125+
}
126+
}
127+
}
128+
return len(stk)
129+
}
60130
```
61131

62132
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int minAddToMakeValid(string s) {
4+
stack<char> stk;
5+
for (char& c: s)
6+
{
7+
if (c == '(') stk.push(c);
8+
else
9+
{
10+
if (!stk.empty() && stk.top() == '(') {
11+
stk.pop();
12+
}
13+
else stk.push(c);
14+
}
15+
}
16+
return stk.size();
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minAddToMakeValid(s string) int {
2+
var stk []rune
3+
for _, c := range s {
4+
if c == '(' {
5+
stk = append(stk, c)
6+
} else {
7+
if len(stk) > 0 && stk[len(stk)-1] == '(' {
8+
stk = stk[:len(stk)-1]
9+
} else {
10+
stk = append(stk, c)
11+
}
12+
}
13+
}
14+
return len(stk)
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int minAddToMakeValid(String s) {
3+
Deque<Character> stk = new ArrayDeque<>();
4+
for (char c : s.toCharArray()) {
5+
if (c == '(') {
6+
stk.push(c);
7+
} else {
8+
if (!stk.isEmpty() && stk.peek() == '(') {
9+
stk.pop();
10+
} else {
11+
stk.push(c);
12+
}
13+
}
14+
}
15+
return stk.size();
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def minAddToMakeValid(self, s: str) -> int:
3+
stk = []
4+
for c in s:
5+
if c == '(':
6+
stk.append(c)
7+
else:
8+
if stk and stk[-1] == '(':
9+
stk.pop()
10+
else:
11+
stk.append(c)
12+
return len(stk)

0 commit comments

Comments
 (0)