# Leetcode 1190. Reverse Substrings Between Each Pair of Parentheses 給定一個字串s,將其中小括號包起來的部分字串反轉。 ## 想法 ### 堆疊 將字符依次放入stack中,當遇到")"時,從stack中開始拿出字符,直到遇到"(",將拿出的字符反轉後,在放入stack中,最後將stack從正向組合成字串回傳。 程式碼: ``` def reverseParentheses(self, s: str) -> str: stack = [] for i in s: if(i==")"): buffer = "" while(stack[-1]!="("): buffer += stack.pop()[::-1] stack.pop() stack.append(buffer) else: stack.append(i) return "".join(stack) ```