Skip to content

Commit ff74483

Browse files
committed
feat: add solutions to lc problem: No.1190
No.1190.Reverse Substrings Between Each Pair of Parentheses
1 parent 491d1a4 commit ff74483

File tree

7 files changed

+488
-135
lines changed

7 files changed

+488
-135
lines changed

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md

Lines changed: 194 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,21 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
用双端队列或者栈,模拟反转过程
59+
**方法一:模拟**
60+
61+
用双端队列或者栈,模拟反转的过程。
62+
63+
时间复杂度 $O(n^2)$,其中 $n$ 为字符串 $s$ 的长度。
64+
65+
**方法二:脑筋急转弯**
66+
67+
我们观察发现,遍历字符串时,每一次遇到 `(` 或者 `)`,都是跳到对应的 `)` 或者 `(`,然后反转遍历的方向,继续遍历。
68+
69+
因此,我们可以用一个数组 $d$ 来记录每个 `(` 或者 `)` 对应的另一个括号的位置,即 $d[i]$ 表示 $i$ 处的括号对应的另一个括号的位置。直接用栈就可以求出 $d$ 数组。
70+
71+
然后,我们从左到右遍历字符串,遇到 `(` 或者 `)` 时,根据 $d$ 数组跳到对应的位置,然后反转方向,继续遍历,直到遍历完整个字符串。
72+
73+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
6074

6175
<!-- tabs:start -->
6276

@@ -67,17 +81,41 @@
6781
```python
6882
class Solution:
6983
def reverseParentheses(self, s: str) -> str:
70-
stack = []
84+
stk = []
7185
for c in s:
72-
if c == ")":
73-
tmp = []
74-
while stack[-1] != "(":
75-
tmp += stack.pop()
76-
stack.pop()
77-
stack += tmp
86+
if c == ')':
87+
t = []
88+
while stk[-1] != '(':
89+
t.append(stk.pop())
90+
stk.pop()
91+
stk.extend(t)
92+
else:
93+
stk.append(c)
94+
return ''.join(stk)
95+
```
96+
97+
```python
98+
class Solution:
99+
def reverseParentheses(self, s: str) -> str:
100+
n = len(s)
101+
d = [0] * n
102+
stk = []
103+
for i, c in enumerate(s):
104+
if c == '(':
105+
stk.append(i)
106+
elif c == ')':
107+
j = stk.pop()
108+
d[i], d[j] = j, i
109+
i, x = 0, 1
110+
ans = []
111+
while i < n:
112+
if s[i] in '()':
113+
i = d[i]
114+
x = -x
78115
else:
79-
stack.append(c)
80-
return "".join(stack)
116+
ans.append(s[i])
117+
i += x
118+
return ''.join(ans)
81119
```
82120

83121
### **Java**
@@ -87,27 +125,140 @@ class Solution:
87125
```java
88126
class Solution {
89127
public String reverseParentheses(String s) {
90-
Deque<Character> deque = new ArrayDeque<>();
91-
for (char c : s.toCharArray()) {
128+
int n = s.length();
129+
int[] d = new int[n];
130+
Deque<Integer> stk = new ArrayDeque<>();
131+
for (int i = 0; i < n; ++i) {
132+
if (s.charAt(i) == '(') {
133+
stk.push(i);
134+
} else if (s.charAt(i) == ')') {
135+
int j = stk.pop();
136+
d[i] = j;
137+
d[j] = i;
138+
}
139+
}
140+
StringBuilder ans = new StringBuilder();
141+
int i = 0, x = 1;
142+
while (i < n) {
143+
if (s.charAt(i) == '(' || s.charAt(i) == ')') {
144+
i = d[i];
145+
x = -x;
146+
} else {
147+
ans.append(s.charAt(i));
148+
}
149+
i += x;
150+
}
151+
return ans.toString();
152+
}
153+
}
154+
```
155+
156+
### **C++**
157+
158+
```cpp
159+
class Solution {
160+
public:
161+
string reverseParentheses(string s) {
162+
string stk;
163+
for (char& c : s) {
92164
if (c == ')') {
93-
StringBuilder sb = new StringBuilder();
94-
while (deque.peekLast() != '(') {
95-
sb.append(deque.pollLast());
96-
}
97-
deque.pollLast();
98-
for (int i = 0, n = sb.length(); i < n; i++) {
99-
deque.offerLast(sb.charAt(i));
165+
string t;
166+
while (stk.back() != '(') {
167+
t.push_back(stk.back());
168+
stk.pop_back();
100169
}
170+
stk.pop_back();
171+
stk += t;
101172
} else {
102-
deque.offerLast(c);
173+
stk.push_back(c);
174+
}
175+
}
176+
return stk;
177+
}
178+
};
179+
```
180+
181+
```cpp
182+
class Solution {
183+
public:
184+
string reverseParentheses(string s) {
185+
int n = s.size();
186+
vector<int> d(n);
187+
stack<int> stk;
188+
for (int i = 0; i < n; ++i) {
189+
if (s[i] == '(') {
190+
stk.push(i);
191+
} else if (s[i] == ')') {
192+
int j = stk.top();
193+
stk.pop();
194+
d[i] = j;
195+
d[j] = i;
103196
}
104197
}
105-
StringBuilder sb = new StringBuilder();
106-
while (!deque.isEmpty()) {
107-
sb.append(deque.pollFirst());
198+
int i = 0, x = 1;
199+
string ans;
200+
while (i < n) {
201+
if (s[i] == '(' || s[i] == ')') {
202+
i = d[i];
203+
x = -x;
204+
} else {
205+
ans.push_back(s[i]);
206+
}
207+
i += x;
108208
}
109-
return sb.toString();
209+
return ans;
110210
}
211+
};
212+
```
213+
214+
### **Go**
215+
216+
```go
217+
func reverseParentheses(s string) string {
218+
stk := []byte{}
219+
for i := range s {
220+
if s[i] == ')' {
221+
t := []byte{}
222+
for stk[len(stk)-1] != '(' {
223+
t = append(t, stk[len(stk)-1])
224+
stk = stk[:len(stk)-1]
225+
}
226+
stk = stk[:len(stk)-1]
227+
stk = append(stk, t...)
228+
} else {
229+
stk = append(stk, s[i])
230+
}
231+
}
232+
return string(stk)
233+
}
234+
```
235+
236+
```go
237+
func reverseParentheses(s string) string {
238+
n := len(s)
239+
d := make([]int, n)
240+
stk := []int{}
241+
for i, c := range s {
242+
if c == '(' {
243+
stk = append(stk, i)
244+
} else if c == ')' {
245+
j := stk[len(stk)-1]
246+
stk = stk[:len(stk)-1]
247+
d[i], d[j] = j, i
248+
}
249+
}
250+
ans := []byte{}
251+
i, x := 0, 1
252+
for i < n {
253+
if s[i] == '(' || s[i] == ')' {
254+
i = d[i]
255+
x = -x
256+
} else {
257+
ans = append(ans, s[i])
258+
}
259+
i += x
260+
}
261+
return string(ans)
111262
}
112263
```
113264

@@ -119,33 +270,32 @@ class Solution {
119270
* @return {string}
120271
*/
121272
var reverseParentheses = function (s) {
122-
let stack = [];
123-
let hashMap = {};
124273
const n = s.length;
125-
for (let i = 0; i < n; i++) {
126-
let cur = s.charAt(i);
127-
if (cur == '(') {
128-
stack.push(i);
129-
} else if (cur == ')') {
130-
let left = stack.pop();
131-
hashMap[left] = i;
132-
hashMap[i] = left;
274+
const d = new Array(n).fill(0);
275+
const stk = [];
276+
for (let i = 0; i < n; ++i) {
277+
if (s[i] == '(') {
278+
stk.push(i);
279+
} else if (s[i] == ')') {
280+
const j = stk.pop();
281+
d[i] = j;
282+
d[j] = i;
133283
}
134284
}
135-
let res = [];
136285
let i = 0;
137-
let step = 1; // 1向右,-1向左
138-
while (i > -1 && i < n) {
139-
let cur = s.charAt(i);
140-
if (cur == '(' || cur == ')') {
141-
step = -step;
142-
i = hashMap[i];
286+
let x = 1;
287+
const ans = [];
288+
while (i < n) {
289+
const c = s.charAt(i);
290+
if (c == '(' || c == ')') {
291+
i = d[i];
292+
x = -x;
143293
} else {
144-
res.push(cur);
294+
ans.push(c);
145295
}
146-
i += step;
296+
i += x;
147297
}
148-
return res.join('');
298+
return ans.join('');
149299
};
150300
```
151301

0 commit comments

Comments
 (0)