Leetcode-5016 Remove Outermost Parentheses(删除最外层的括号)

 1 class Solution
 2 {
 3     public:
 4         string removeOuterParentheses(string S)
 5         {
 6             string rnt;
 7             stack<char> s;
 8             s.push(S[0]);
 9             
10             for(int i = 1; i < S.size();i ++)
11             {
12                 if(S[i]=='(')
13                 {
14                     s.push(S[i]);
15                     rnt += '(';
16                 }
17                 else if(s.size()>1 && S[i]==')')
18                 {
19                     s.pop();
20                     rnt += ')';
21                 }
22                 else if(s.size()==1 && S[i]==')')
23                 {
24                     s.pop();
25                     s.push('(');
26                     i ++;
27                 }
28             }
29             return rnt;
30         }
31 };

栈的应用

原文地址:https://www.cnblogs.com/Asurudo/p/10665067.html