forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
42 lines (37 loc) · 946 Bytes
/
Solution.java
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
import java.util.Stack;
/**
* @author Anonymous
* @since 2019/11/22
*/
public class Solution {
/**
* 判断是否是弹出序列
*
* @param pushA 压栈序列
* @param popA 弹栈序列
* @return 是否是弹出序列
*/
public boolean IsPopOrder(int[] pushA,int[] popA) {
if (pushA == null || popA == null || pushA.length != popA.length) {
return false;
}
Stack<Integer> stack = new Stack<>();
int i = 0;
int n = pushA.length;
boolean flag = false;
for (int val : popA) {
while (stack.isEmpty() || stack.peek() != val) {
if (i >= n) {
flag = true;
break;
}
stack.push(pushA[i++]);
}
if (flag) {
break;
}
stack.pop();
}
return stack.isEmpty();
}
}