We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Do not use the Stack class, since it has been deprecated for a while. Instead use the ArrayDeque class which is meant to replace it. Even the JDK documentation suggests the same:
- switch-case blocks are generally more readable, and at the same time easily optimized by compiler and JVM (at runtime).
- You can combine the peek and the pop into a single operation. When you see a closing bracket, if either the stack is empty or the top of the stack (after pop) does not match you fail right away. If it matches, the pop is still applicable, and you continue with your iteration. Look at this code snippet (I'm pasting only the helper method)
Java Stack
You are viewing a single comment's thread. Return to all comments →
Couple of suggestions for improvement.
- Do not use the
Stack
class, since it has been deprecated for a while. Instead use theArrayDeque
class which is meant to replace it. Even the JDK documentation suggests the same:- switch-case blocks are generally more readable, and at the same time easily optimized by compiler and JVM (at runtime).
- You can combine the peek and the pop into a single operation. When you see a closing bracket, if either the stack is empty or the top of the stack (after pop) does not match you fail right away. If it matches, the pop is still applicable, and you continue with your iteration. Look at this code snippet (I'm pasting only the helper method)