栈,队列和双端队列

栈:后进先出

队列:先进先出

双端队列:两边都可以插入和删除

 

1. 匹配分隔符(应用的原理:栈)

 1 def is_matched(expr):
 2     """return true if delimiters are properly match;False otherwise"""
 3     lefty = '({['
 4     rghty = ')}]'
 5     S = ArrayStack()
 6     for c in expr:
 7         if c in lefty:
 8             S.push(c)
 9         elif c in righty:
10             if S.is_empty():
11                 return False
12             if righty.index(c)!=lefty.index(S.pop()):
13                 return False
14     return S.is_empty()

 

 

比较灵巧的部分我觉得是12行,S.pop()其实写成c也是一样的,但是如果是S.pop()的话就去掉里面的已经匹配好的括号了,然后下面就不需要再删除

 

posted @ 2019-03-11 20:19  洛圣熙  阅读(529)  评论(0编辑  收藏  举报