FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Algorithm/StackPushPopOrder.cpp at master · Tanswer/Algorithm · GitHub
Tanswer
/
Algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Algorithm
/
StackPushPopOrder.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (48 loc) · 1.29 KB
Breadcrumbs
Algorithm
/
StackPushPopOrder.cpp
Copy path
File metadata and controls
60 lines (48 loc) · 1.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
60
/*
* @filename: StackPushPopOrder.cpp
* @author: Tanswer
* @date: 2018年02月25日 14:18:06
* @description:
*/
#
include
<
iostream
>
#
include
<
stack
>
using
namespace
std
;
bool
IsPopOrder
(
const
int
* pPush,
const
int
* pPop,
int
nLength)
{
bool
bPossible =
false
;
if
(pPush !=
NULL
&& pPop !=
NULL
&& nLength !=
0
)
{
const
int
* pNextPush = pPush;
const
int
* pNextPop = pPop;
stack<
int
> dataStack;
//
遍历判断输出序列
while
(pNextPop - pPop < nLength)
{
//
while
(dataStack.
empty
() || dataStack.
top
() != *pNextPop)
{
//
所有数字都压栈了
if
(pNextPush - pPush == nLength)
break
;
dataStack.
push
(*pNextPush);
pNextPush++;
}
if
(dataStack.
top
() != *pNextPop)
break
;
dataStack.
pop
();
pNextPop++;
}
if
(dataStack.
empty
() && pNextPop - pPop == nLength)
bPossible =
true
;
}
return
bPossible;
}
int
main
()
{
int
push[
5
] = {
1
,
2
,
3
,
4
,
5
};
int
pop1[
5
] = {
4
,
5
,
3
,
2
,
1
};
int
pop2[
5
] = {
4
,
3
,
5
,
1
,
2
};
cout <<
IsPopOrder
(push,pop1,
5
) << endl;
cout <<
IsPopOrder
(push,pop2,
5
) << endl;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL