Open rocksc30 opened 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(); } }