-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDanDiaoStack.java
More file actions
52 lines (44 loc) · 1.04 KB
/
DanDiaoStack.java
File metadata and controls
52 lines (44 loc) · 1.04 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
import java.util.*;
public class DanDiaoStack{
private Stack<Integer> stack;
private int[] arr;
public DanDiaoStack(int[] _arr){
stack=new Stack<Integer>();
arr = _arr;
}
public int[][] getResult(){
int[][] result = new int[arr.length][2];
for (int i=0; i<arr.length;i++ ) {
if(stack.isEmpty()){
stack.push(i);
}else{
while(!stack.isEmpty()){
if(arr[stack.peek()] > arr[i]){
break;
}
int cur = stack.pop();
result[cur][0] = stack.isEmpty() ? -1:stack.peek();
result[cur][1] = i;
}
stack.push(i);
}
}
while(!stack.isEmpty()){
int cur = stack.pop();
result[cur][0] = stack.isEmpty() ? -1:stack.peek();
result[cur][1] = -1;
}
return result;
}
public static void main(String[] args){
int[] arr= new int[]{2,3,5,1,6,0,7,9};
DanDiaoStack dan=new DanDiaoStack(arr);
int[][] result = dan.getResult();
for (int i=0; i<result.length;i++ ) {
for (int j=0; j<result[i].length; j++) {
System.out.print(result[i][j]+",");
}
System.out.println();
}
}
}