In a deck of cards, every card has a unique integer. You can order the deck in any order you want.
Initially, all the cards start face down (unrevealed) in one deck.
Now, you do the following steps repeatedly, until all cards are revealed:
- Take the top card of the deck, reveal it, and take it out of the deck.
- If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
- If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
The first entry in the answer is considered to be the top of the deck.
给定一个数组deck,实际上是一副牌,然后从数组的头开始翻牌,翻一张牌再将下一张牌放置最后。
题目是寻找一个数组,使得其进行这种翻牌之后依次翻出来的牌是从小到大排列的。
我们可以逆向思维,先将原来的牌堆排序,从原来的牌堆中拿出最后一张牌放到新牌堆的第一张,再将新
牌堆的最后一张放到新牌堆的最前面,此时再将新牌堆的最后一张拿出来,再从原牌堆最后取出一
张牌进行同样的操作。
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
sort(deck.begin(), deck.end());
list<int> k;
for (int i = deck.size() - 1; i > 0; i--)
{
k.push_front(deck[i]);
k.push_front(k.back());
k.pop_back();
}
k.push_front(deck[0]);
vector<int> res(begin(k), end(k));
return res;
}
};