rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

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

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago

class Solution {
    public String reverseParentheses(String s) {
        char[] cs = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        LinkedList<Character> queue = new LinkedList<>();

        for (int i = 0; i < cs.length; i++) {
            if (cs[i] != ')'){
                stack.push(cs[i]);
            }else {
                while (stack.peek() != '('){
                    queue.offer(stack.pop());
                }
                stack.pop();
                while (!queue.isEmpty()){
                    stack.push(queue.poll());
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}