Leetcode学习笔记:#950. Reveal Cards In Increasing Order

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:

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.

实现:

public int[] deckRevealedIncreasing(int[] deck){
	int n = deck.length;
	Arrays.sort(deck);
	Queue<Integer> q = new LinkedList<>();
	for(int i = 0; i <n; i++) 
		q.add(i);
	int[] res = new int[n];
	for(int i = 0; i<n;i++){
		res[q.poll()]=deck[i];
		q.add(q.poll());
	}
	return res;
}	

思路:
对deck排序,再创建一个队列插入0-n位数字。迭代求res数组,第一位res[0] = deck[0],第二位res[1]挪到队列最后,此为一次循环,第三位res[2] = deck[2],第四位res[3]挪到队列最后,以此类推。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值