LeetCode #950. Reveal Cards In Increasing Order

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:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
  3. 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;
	}
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值