其他分享
首页 > 其他分享> > 1190. 反转每对括号间的子串

1190. 反转每对括号间的子串

作者:互联网

给出一个字符串 s(仅含有小写英文字母和括号)。

请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。

注意,您的结果中 不应 包含任何括号。

 

示例 1:

输入:s = "(abcd)"
输出:"dcba"

示例 2:

输入:s = "(u(love)i)"
输出:"iloveu"

示例 3:

输入:s = "(ed(et(oc))el)"
输出:"leetcode"

示例 4:

输入:s = "a(bcdefghijkl(mno)p)q"
输出:"apmnolkjihgfedcbq"

 

提示:

 

class Solution {
public:
    string reverseParentheses(string s) {
        stack<int> st;
        string res;
        for(auto c:s){
            if(c=='(')st.push(res.length());
            else if(c==')'){
                int t=st.top();
                st.pop();
                reverse(res.begin()+t,res.end());
            }
            else res+=c;
        }
        return res;
    }
};

 

标签:子串,string,示例,1190,st,括号,res,输入
来源: https://www.cnblogs.com/xxxsans/p/14820639.html