其实就是栈操作能否按照给定顺序输出
public static String getResult(int[] res){ if (res.length <= 2){ return "Yes"; } Deque<Integer> de = new LinkedList<>(); int s = 1; int k = res[0]; de.push(1); boolean[] flag = new boolean[res.length]; while (de.peek() != k){ flag[s-1] = true; de.push(++s); } for (int i = 0; i < res.length; i++) { int t = res[i]; if (de.isEmpty()){ de.push(t); flag[t-1] = true; }else if (t == de.peek()){ de.pop(); }else { if (flag[t-1]){ return "No"; } de.push(t); flag[t-1] = true; } } return "Yes"; }