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"
提示:
0 <= s.length <= 2000
s 中只有小写英文字母和括号
我们确保所有括号都是成对出现的
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/* a(bcdefghijkl(mno)p)q a [‘a‘] ( [‘a‘, ‘‘] b [‘a‘, ‘b‘] c [‘a‘, ‘bc‘] d [‘a‘, ‘bcd‘] e [‘a‘, ‘bcde‘] f [‘a‘, ‘bcdef‘] g [‘a‘, ‘bcdefg‘] h [‘a‘, ‘bcdefgh‘] i [‘a‘, ‘bcdefghi‘] j [‘a‘, ‘bcdefghij‘] k [‘a‘, ‘bcdefghijk‘] l [‘a‘, ‘bcdefghijkl‘] ( [‘a‘, ‘bcdefghijkl‘, ‘‘] m [‘a‘, ‘bcdefghijkl‘, ‘m‘] n [‘a‘, ‘bcdefghijkl‘, ‘mn‘] o [‘a‘, ‘bcdefghijkl‘, ‘mno‘] ) [‘a‘, ‘bcdefghijklonm‘] p [‘a‘, ‘bcdefghijklonmp‘] ) [‘apmnolkjihgfedcb‘] q [‘apmnolkjihgfedcbq‘] 作者:tuotuoli 链接:https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/solution/1190-fan-zhuan-mei-dui-gua-hao-jian-de-zi-chuan-ji/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ //思路 //temp为当前输入的连在一起的字符串 //若是字符 则放到temp后 //若遇到 左括号 将temp入栈 temp = "" //若遇到 右括号 令temp = top + reverse(temp) class Solution { public: string reverseParentheses(string s) { stack<string> my_stack; string temp=""; for (char ch : s) { if (ch == ‘(‘) { my_stack.push(temp); temp = ""; } else if (ch == ‘)‘) { reverse(temp.begin(), temp.end()); temp = my_stack.top() + temp; my_stack.pop(); } else temp += ch; } return temp; } };
相关推荐
小焊猪web前端 2020-11-04
LULUBAO 2020-07-26
shengge0 2020-06-01
jyj00 2020-05-15
qidu 2020-05-15
shenwenjie 2020-03-20
leap 2020-03-01
luofuIT成长记录 2020-02-09
RuoShangM 2020-01-12
RuoShangM 2020-01-12
ustbfym 2020-01-04
lauwen 2019-12-27
Airuio 2019-12-23
Winterto0 2019-12-23
Darklovy 2019-12-17
jyj00 2019-12-15
nimeijian 2019-12-09
rootdream 2019-12-05