-
Notifications
You must be signed in to change notification settings - Fork 5
/
1021.cpp
48 lines (40 loc) · 905 Bytes
/
1021.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <stack>
using namespace std;
/*
Runtime: 44 ms, faster than 5.42% of C++ online submissions for Remove Outermost Parentheses.
Memory Usage: 6.8 MB, less than 22.19% of C++ online submissions for Remove Outermost Parentheses.
*/
class Solution {
public:
string removeOuterParentheses(string s) {
stack<char> stack_for_s;
string result = s;
int start_index, end_index;
for (int i = 0; i < result.size(); i++)
{
if (result.at(i) == '(') {
if (stack_for_s.size() == 0) {
start_index = i;
}
stack_for_s.push('(');
}
else {
stack_for_s.pop();
if (stack_for_s.size() == 0) {
end_index = i;
result.erase(start_index, 1);
result.erase(end_index - 1, 1);
i -= 2;
}
}
}
return result;
}
};
int main(void) {
Solution sol = Solution();
sol.removeOuterParentheses("()()");
return 0;
}