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.

Example 1:

Input: [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.

Note:

  1. 1 <= A.length <= 1000
  2. 1 <= A[i] <= 10^6
  3. A[i] != A[j] for all i != j

Idea 1. 官方的妙法,直接由答案数组来模拟游戏,有点意思

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
int n = deck.length;
int[] cards = new int[n];
Deque<Integer> deq = new ArrayDeque<>();
for(int i = 0; i < n; ++i) {
deq.addLast(i);
} Arrays.sort(deck); for(int j = 0; j < n; ++j){
int i = deq.removeFirst();
cards[i] = deck[j];
if(!deq.isEmpty()) {
deq.addLast(deq.removeFirst());
}
} return cards;
}
}

Reveal Cards In Increasing Order LT950的更多相关文章

  1. [Swift]LeetCode950. 按递增顺序显示卡牌 | 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. ...

  2. [Solution] 950. Reveal Cards In Increasing Order

    Difficulty: Medium Problem In a deck of cards, every card has a unique integer. You can order the de ...

  3. 113th LeetCode Weekly Contest 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. ...

  4. 【leedcode】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 ...

  5. 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  6. 揭示牌面使之升序 Reveal Cards In Increasing Order

    2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...

  7. Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌

    牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...

  8. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  9. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...

随机推荐

  1. jQuery入门基础(事件、DOM操作)

    http://www.w3school.com.cn/b.asp 一.事件 1.常规事件——把js事件前面的on去掉 比如:js:onclick——JQuery:click 下面是 jQuery 中事 ...

  2. Laravel--查询构造器 方法 及聚合函数

    controller内内容如下 <?php /** * Created by PhpStorm. * User: work * Date: 2018/1/2 * Time: 20:36 */ n ...

  3. c#switch语句的用法

    switch条件语句是一种很常用的选择语句,它与if条件语句不同,它只针对某个表达式的值作出判断,从而决定执行哪一段代码. switch条件语句用到的关键字: switch case break de ...

  4. 500G !!史上最全的JAVA全套教学视频网盘分享 (JEECG开源社区)

    500 G JAVA视频网盘分享(JEECG开源社区) [涵盖从java入门到深入架构,Linux.云计算.分布式.大数据Hadoop.ios.Android.互联网技术应有尽有] JEECG开源社区 ...

  5. Ajax需要带头信息跨域问题的解决

    $.ajax({ type:"get", url:"http://localhost:8082/index/getMsg", dataType:'json', ...

  6. Python学习日记 --day4

    list列表: # # 列表 ''' # 增 # li = [1,'qwer','小明',[1,2,'qwe']] # print(li[0],type(li)) # 跟字符串的操作方法是一样的,返回 ...

  7. window Linux 双系统安装

    我是先安装的win10,然后在其基础上又安装了Ubuntu 16.04,为了今后再次安装方便,这里记录一下安装过程. 我在安装时主要参考了文章:https://blog.csdn.net/flyyuf ...

  8. linux 之 shell

    echo 输出命令 语法:echo [选项][输出内容] 选项 -e:支持反斜线控制的字符转换 范例 vi hello.sh #!/bin/bash #author liuch echo " ...

  9. Chen qiaoqiao Studio

    Welcome here! If you need any help, please contact us. Contact info Email: lovey_kids@163.com

  10. js中的“默默的失败”

    看阮一峰的js标准教程,看到了“默默的失败”觉得很形象也很无奈, 总结一下都有哪些地方会“默默的失败” 字符串内部的单个字符无法改变和增删,这些操作会默默地失败. var s = 'hello'; d ...