-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_pop_order.py
41 lines (37 loc) · 898 Bytes
/
is_pop_order.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
def isPopOrder(pushV, popV):
if len(pushV) == 0 or len(pushV) != len(popV):
return False
stack = []
j = 0
for v in pushV:
stack.append(v)
if stack[-1] == popV[j]:
stack.pop()
j += 1
while stack:
if stack.pop() == popV[j]:
j += 1
else:
return False
return True
def isPopOrder1(pushV, popV):
if len(pushV) == 0 or len(pushV) != len(popV):
return False
stack = []
j = 0
for v in pushV:
stack.append(v)
while stack and stack[-1] == popV[j]:
stack.pop()
j += 1
if stack:
return False
else:
return True
data = [1, 2, 3, 4, 5]
right = [4, 5, 3, 2, 1]
wrong = [4, 3, 5, 1, 2]
print(isPopOrder(data, right))
print(isPopOrder(data, wrong))
print(isPopOrder1(data, right))
print(isPopOrder1(data, wrong))