-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathArrayStack.py
59 lines (43 loc) · 1.32 KB
/
ArrayStack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from base import StackBase
from Array import Array
class ArrayStack(StackBase):
"""使用自定义的Array实现"""
def __init__(self, capacity=0):
self._array = Array(capacity=capacity)
def get_size(self):
return self._array.get_size()
def is_empty(self):
return self._array.is_empty()
def get_capacity(self):
return self._array.get_capacity()
def __str__(self):
return str('<chapter_05_Stack_Queue.stack.ArrayStack> : {}'.format(self._array))
def __repr__(self):
return self.__str__()
def push(self, e):
self._array.add_last(e)
def pop(self):
return self._array.remove_last()
def peek(self):
return self._array.get_last()
if __name__ == '__main__':
# 括号匹配
def is_valid(input_str):
left_ = set(['(', '[', '{'])
hash_ = {
')': '(',
']': '[',
'}': '{',
}
stack = ArrayStack()
for ch in input_str:
if ch in left_:
stack.push(ch)
else:
if stack.get_size() == 0 or hash_[ch] != stack.pop():
return False
return stack.is_empty()
input_str1 = '[{(())}]'
print(is_valid(input_str1))
input_str1 = '[{(())})'
print(is_valid(input_str1))