forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1190.cpp
More file actions
30 lines (29 loc) · 678 Bytes
/
1190.cpp
File metadata and controls
30 lines (29 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution
{
public:
string reverseParentheses(string s)
{
vector<int> st;
unordered_map<int, int> pa;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == '(') st.push_back(i);
if (s[i] == ')')
{
auto j = st.back(); st.pop_back();
pa[i] = j; pa[j] = i;
}
}
string res;
int i = 0, d = 1, sLen = s.size();
while (i < sLen)
{
if (s[i] == ')' or s[i] == '(')
{
i = pa[i]; d = -d;
} else res += s[i];
i += d;
}
return res;
}
};