Skip to content

Commit 4b7c2d8

Browse files
committed
practice
1 parent d7b6cdf commit 4b7c2d8

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

StackPushPopOrder.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @filename: StackPushPopOrder.cpp
3+
* @author: Tanswer
4+
* @date: 2018年02月25日 14:18:06
5+
* @description:
6+
*/
7+
8+
#include <iostream>
9+
#include <stack>
10+
using namespace std;
11+
12+
13+
bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
14+
{
15+
bool bPossible = false;
16+
17+
if(pPush != NULL && pPop != NULL && nLength != 0)
18+
{
19+
const int* pNextPush = pPush;
20+
const int* pNextPop = pPop;
21+
22+
stack<int> dataStack;
23+
24+
// 遍历判断输出序列
25+
while(pNextPop - pPop < nLength)
26+
{
27+
//
28+
while(dataStack.empty() || dataStack.top() != *pNextPop)
29+
{
30+
// 所有数字都压栈了
31+
if(pNextPush - pPush == nLength)
32+
break;
33+
34+
dataStack.push(*pNextPush);
35+
pNextPush++;
36+
}
37+
38+
if(dataStack.top() != *pNextPop)
39+
break;
40+
41+
dataStack.pop();
42+
pNextPop++;
43+
}
44+
45+
if(dataStack.empty() && pNextPop - pPop == nLength)
46+
bPossible = true;
47+
}
48+
return bPossible;
49+
}
50+
51+
int main()
52+
{
53+
int push[5] = {1,2,3,4,5};
54+
int pop1[5] = {4,5,3,2,1};
55+
int pop2[5] = {4,3,5,1,2};
56+
57+
cout << IsPopOrder(push,pop1,5) << endl;
58+
cout << IsPopOrder(push,pop2,5) << endl;
59+
return 0;
60+
}

0 commit comments

Comments
 (0)