LC.P950[按递增顺序显示卡牌]

方法一:队列模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
int n = deck.length;
Deque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < n; ++i) queue.offer(i);
int[] ans = new int[n];
Arrays.sort(deck);
for (int x : deck) {
ans[queue.poll()] = x;
if (!queue.isEmpty()) queue.offer(queue.poll());
}
return ans;
}
}
  • 时间复杂度:$O(nlogn)$
  • 空间复杂度:$O(n)$