AcWing 830. Java 单调栈 ArrayDeque & StringBuilder
原题链接
简单
作者:
YYC
,
2020-07-06 18:44:30
,
所有人可见
,
阅读 430
import java.util.*;
public class Main{
public static void main(String... args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayDeque<Integer> s = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
while (n-- > 0){
int cur = in.nextInt();
while (!s.isEmpty() && s.peek() >= cur){
s.pop();
}
if (s.isEmpty()){
sb.append(-1).append(' ');
} else {
sb.append(s.peek()).append(' ');
}
s.push(cur);
}
System.out.println(sb.toString().trim());
}
}