-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJudgeStackOrder22.java
More file actions
92 lines (86 loc) · 2.87 KB
/
JudgeStackOrder22.java
File metadata and controls
92 lines (86 loc) · 2.87 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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package offer;
import java.util.*;
public class JudgeStackOrder22 {
/*
*
*/
public static boolean IsPopOrder(int[] pushA, int[] popA) {
if (pushA.length <= 0 || popA.length <= 0) {
return false;
}
int j = 0;
int i = 0;
Stack<Integer> s = new Stack<Integer>();
for (; i < popA.length; i++) {
while (s.isEmpty() || s.peek() != popA[i]) {
if (j < pushA.length) {
s.add(pushA[j]);
j++;
} else {
// j ����pushA�ij���
break;
}
}
// �ڶ��֣���ջ����û���ҵ�һ������ջԪ����ȵ�Ԫ�أ�
// ��������ջ��Ԫ���Ѿ�ȫ����ջ��
// break ������
if (s.peek() != popA[i]) {
break;
}
// ��һ�֣���ջ�����ҵ���һ������ջԪ����ȵ�Ԫ��
s.pop();
}
if (s.isEmpty()) {
return true;
} else
return false;
}
/*
* pushA 入栈序列,popA 出栈序列
*/
public static boolean IsPopOrder2(int[] pushA, int[] popA) {
if (pushA.length <= 0 || popA.length <= 0) {
return false;
}
Stack<Integer> s = new Stack<Integer>();
int i = 0, j = 0;
for (i = 0, j = 0; i < pushA.length; i++) {
// 一直要将入队元素 全部入队
s.push(pushA[i]);
// 直到碰到相等那么j++,且弹出
// while用于将所有已经能匹配的全部出栈
// 如果j == popA.length 其实也是对的,说明,入栈没有完全出栈
// 如输入:1 2 3 4 5 6出栈:1 2 3 4 5:对的 true
// 但是 入 1 2 3 4 出 1 2 3 4 5 false
/*
* 改进版本
*/
while (j < popA.length && !s.isEmpty() && s.peek() == popA[j]) {
s.pop();
j++;
}
}
// return s.isEmpty();
/*
*/
if (j == popA.length) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int[] push = { 1, 2, 3, 4, 5, 6 , 7 };
int[] pop1 = { 1,2,3,4,5};
/*
* int[] pop2 = {3, 5, 4, 2, 1}; int[] pop3 = {4, 3, 5, 1, 2}; int[]
* pop4 = {3, 5, 4, 1, 2};
*/
System.out.println("true: " + IsPopOrder2(push, pop1));
/*
* System.out.println("true: " + IsPopOrder2(push, pop2));
* System.out.println("false: " + IsPopOrder2(push, pop3));
* System.out.println("false: " + IsPopOrder2(push, pop4));
*/
}
}