这个说明, 我愿称之为世界上最难读懂的两句话
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for(int i = 0; i<num; i++){
arr[i] = sc.nextInt();
}
LinkedList<int[]> stack = new LinkedList<>();
int[] resArr = new int[num];
for(int i = 0; i<num; i++){
int cur = arr[i];
while(true){
// 如果是空直接加入
if(stack.isEmpty()){
stack.addLast(new int[]{cur,i});
break;
}
int[] peekArr = stack.getLast();
int peekValue = peekArr[0];
int peekIndex = peekArr[1];
if(cur>peekValue){
resArr[peekIndex] = i;
stack.removeLast();
} else {
stack.addLast(new int[]{cur, i});
break;
}
}
}
StringJoiner str = new StringJoiner(" ");
for(int v : resArr){
str.add(v+"");
}
System.out.println(str.toString());
}
}