-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain31.java
More file actions
28 lines (25 loc) · 1.03 KB
/
Main31.java
File metadata and controls
28 lines (25 loc) · 1.03 KB
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
package JZOffer2;
import java.util.LinkedList;
/**
* 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。
* 例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
*/
public class Main31 {
public static boolean validateStackSequences(int[] pushed, int[] popped) {
LinkedList<Integer> stack = new LinkedList<>();
int index = 0;
for (int num : pushed) {
stack.push(num);
while (!stack.isEmpty() && stack.peek() == popped[index]) { // 循环判断与出栈
stack.pop();
index++;
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
int[] pushed = {1,2,3,4,5};
int[] popped = {4,5,3,2,1};
validateStackSequences(pushed, popped);
}
}