首页 > 其他 > 详细

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

时间:2021-05-28 09:28:12      阅读:22      评论:0      收藏:0      [点我收藏+]

给出一个字符串 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"

 

提示:

  • 0 <= s.length <= 2000
  • s 中只有小写英文字母和括号
  • 我们确保所有括号都是成对出现的

 

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;
    }
};

 

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

原文:https://www.cnblogs.com/xxxsans/p/14820639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!