File tree Expand file tree Collapse file tree 1 file changed +13
-22
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses Expand file tree Collapse file tree 1 file changed +13
-22
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String reverseParentheses (String s ) {
3
- int n = s .length ();
4
- int [] d = new int [n ];
5
- Deque <Integer > stk = new ArrayDeque <>();
6
- for (int i = 0 ; i < n ; ++i ) {
7
- if (s .charAt (i ) == '(' ) {
8
- stk .push (i );
9
- } else if (s .charAt (i ) == ')' ) {
10
- int j = stk .pop ();
11
- d [i ] = j ;
12
- d [j ] = i ;
13
- }
14
- }
15
- StringBuilder ans = new StringBuilder ();
16
- int i = 0 , x = 1 ;
17
- while (i < n ) {
18
- if (s .charAt (i ) == '(' || s .charAt (i ) == ')' ) {
19
- i = d [i ];
20
- x = -x ;
3
+ StringBuilder stk = new StringBuilder ();
4
+ for (char c : s .toCharArray ()) {
5
+ if (c == ')' ) {
6
+ StringBuilder t = new StringBuilder ();
7
+ while (stk .charAt (stk .length () - 1 ) != '(' ) {
8
+ t .append (stk .charAt (stk .length () - 1 ));
9
+ stk .deleteCharAt (stk .length () - 1 );
10
+ }
11
+ stk .deleteCharAt (stk .length () - 1 );
12
+ stk .append (t );
21
13
} else {
22
- ans .append (s . charAt ( i ) );
14
+ stk .append (c );
23
15
}
24
- i += x ;
25
16
}
26
- return ans .toString ();
17
+ return stk .toString ();
27
18
}
28
- }
19
+ }
You can’t perform that action at this time.
0 commit comments