20-ValidParentheses
Here is the Original Post
Principle
stack: first in & last out 如果左括号入栈,遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后
stack
仍然为空。establish hashtable
dic
construct corresponding relation,key
-left brackets,value
-right brackets, so it’s just O(1) time complexity to check if two brackests are in one-to-one correspondence. Here we build astack
to iterate stirngs
, and judge it by algorithm.
Method
plaintext
1 | st=>start: c |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.