Before considering this question, we first review the data structure of the stack, because the compiler in parentheses match is achieved through the stack:
Stack:
Stack: the latter is advanced out of the data structure; its nature will have a special list of restrictions (last out, top of the stack), lift the stack what we first think of it of course is push (push) and pop (? pop), Let's implement push and pop process through the code:
We should pay attention to this part of the top of the stack: the stack (top), we want to ensure that the stack has only one node, and a link to the following node, the specific implementation is to build a new node, the next new node (the pointer) points the original stack, the stack and then set to the new node. As shown in the following code.
# Create push and pop the stack structure
# First define listnode
class node(object):
def __init__(self,value=None):
self.value=value
self.next=None
class stack(object):
def __init__(self):
self.node=node()
self.top=[self.node]
def push(self,elem):
new_node=node(elem)
if self.top[0].value==None:
new_node.next=None
else:
new_node.next=self.top[0]
self.top.append(new_node)
self.top.pop(0)
def pop(self):
try:
value_top=self.top[0].value
node=self.top[0].next
self.top.append(node)
self.top.pop(0)
return value_top
except:
print('wrong')
s=stack()
for i in ['a','b','c','d']:
s.push(i)
for i in range(4):
print(s.pop())
Issues and Analysis
The problem stems from leetcode,
This problem can be very important, because the compiler program we often want to check whether paired brackets, if we can truly understand this principle, can help us understand the compiler principles:
We first consider the simplest case: when the same type of brackets all the time, we just let each left parenthesis has a right parenthesis and its matching can, in conclusion, we will count the opening parenthesis (left) has the following formula:
left++left++left++
We consider the face of the closing parenthesis, different situations left of:
left = 0 and no description matching right parenthesis left parenthesis, i.e., expression is invaild
left> 0, we have left parenthesis and right parenthesis to match, this time we want to match the left - left - left--
If traversed all the symbols, left! = 0, the expression is still invalid instructions
All in all, we just need to count the opening parenthesis, right parenthesis to see if there is a match, but this is just a simple case, without taking into account the relative position between the different symbols, if such an expression as follows, we summarize the above the law is not satisfied:
({)}
Further consideration
According to an interesting pattern: Zhengzhou Women's Hospital http://www.ytsgfk120.com/
An interesting property on the effective bracket expression is a valid expression of the sub-expressions should also be a valid expression. (Not every sub-expression).
From here we see that is recursive, that is, if each sub-expression is a valid expression, the whole expression is valid, so that we can solve the expression from each sub-expression matches when you delete until the left empty expression illustrate the expression is valid: the algorithm process is as follows:
Left parenthesis encountered it onto the stack.
Encounter a right parenthesis, will launch top of the stack, the stack if the match to continue. If not, you can judge the whole expression is invalid (invaild).
Finally, it shows if the stack is empty, the expression is valid, as follows, using the dictionary data structure will help us reduce the complexity:
# Create push and pop the stack structure
# First define listnode
class node(object):
def __init__(self,value=None):
self.value=value
self.next=None
class stack(object):
def __init__(self):
self.top=None
def push(self,elem):
new_node=node(elem)
if self.top==None:
new_node.next=None
else:
new_node.next=self.top
self.top=new_node
def pop(self):
if self.top!=None:
value_top=self.top.value
node=self.top.next
self.top=node
return value_top
else:
return False
string='()'
dict_={'}':'{',']':'[',')':'('}
def judge_str(str_,dict_):
s=stack()
for i in str_:
if i in dict_:
result=s.pop() if s.top else '#'
if (result!=dict_[i]):
return False
else:
s.push(i)
if s.top==None:
return True
else:
return False
judge_str(string,dict_)
to sum up
In conclusion, we bracket matching is a recursive structure, if each sub-expression matches, then the whole expression is also matched with a stack structure to achieve it, in accordance with the law summarized above, with a dictionary format can be a good solution quickly problem.
Left parenthesis encountered it onto the stack.
Encounter a right parenthesis, will launch top of the stack, the stack if the match to continue. If not, you can judge the whole expression is invalid (invaild)