1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public String reverseParentheses(String s) { Deque<Character> stack = new ArrayDeque<>(); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); if (c == ')') { List<Character> list = new ArrayList<>(); while (stack.peek() != '(') list.add(stack.pop()); stack.pop(); for (Character e : list) { stack.push(e); } } else { stack.push(c); } } StringBuilder builder = new StringBuilder(); while (!stack.isEmpty()) builder.append(stack.removeLast()); return builder.toString(); } }
|