2019-03-27 14:10:37

问题描述:

问题求解:

模拟题。考虑角度是从结果来进行反推。

input - [2,3,5,7,11,13,17] (just sort the input that you get)

The last number that you wanna get is the last number in the array - (17)

The penultimate number is 13. So put 13 on top of 17 (13,17) and bring the last number to top (17,13). Now while you perform the action with (17,13), you will place 17 in the bottom and reveal 13 first - so it becomes (17), now reveal 17.

The number that you want before 13 is 11. Place 11 on top now (11,17,13) and bring the last number to the top (13,11,17). Now when you perfom the action with (13,11,17), you will place 13 in the bottom and reveal 11 - so it becomes (17,13), now you will place 17 in the bottom and reveal 13 - it becomes (17), and then you will reveal 17.

current is 7 -> (7,13,11,17) -> (17,7,13,11) (add 7 to the queue, remove 17 from the queue and add back 17 to the queue)
current is 5 -> (5,17,7,13,11) -> (11,5,17,7,13) (add 5 to the queue, remove 11 from the queue and add back 11 to the queue)
current is 3 -> (3,11,5,17,7,13) -> (13,3,11,5,17,7) (add current to the queue, remove from the queue, add the removed number back to the queue)
current is 2 -> (2,13,3,11,5,17,7) -> Stop here since you don't have anymore numbers.

    public int[] deckRevealedIncreasing(int[] deck) {
int[] res = new int[deck.length];
Arrays.sort(deck);
if (deck.length < 3) return deck;
Deque<Integer> deque = new LinkedList<>();
for (int i = deck.length - 1; i >= 1; i--) {
deque.addFirst(deck[i]);
deque.addFirst(deque.pollLast());
}
deque.addFirst(deck[0]);
for (int i = 0; i < deck.length; i++) res[i] = deque.pollFirst();
return res;
}

  

揭示牌面使之升序 Reveal Cards In Increasing Order的更多相关文章

  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. 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)

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

  4. Reveal Cards In Increasing Order LT950

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...

  5. 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. ...

  6. 【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 ...

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

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

  8. 输入n,然后输入n个数,使它升序输出

    #include<iostream> using namespace std; int main() { int n,i,j,m,k; cin>>n; int a[n];  f ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. Win10部署IIS 10.0

    win10自带IIS10.0 控制面板 >> 程序 >>启用或关闭Windows功能 勾选完之后会安装IIS,安装完成后 计算机管理 >> 服务和应用程序 > ...

  2. Redis入门到高可用(十五)—— GEO

    一.简介 二.应用场景 三.API 1.geoadd 2.geopos 3.geodist 4.georadius 四.相关说明

  3. JUnit单元测试代码

    package com.storage.test; import org.junit.Before; import org.junit.Test; import org.springframework ...

  4. [转] Mac系统Robot Framework环境搭建

    一.由于Mac系统下自带python,所以不需要再进行安装了 二.关闭mac电脑的sip, 1.重启 Mac并长按 Cmd + R 2.打开终端,执行csrutil disable命令 3.重启电脑 ...

  5. Angela启动步骤

    1.在web目录下执行 grunt watch (如果不在目录下执行不能识别,当然首先安装node.js) 2.随便改一个文件,会自动重新生成代码(在dest目录下会生成可执行的代码) 3.如果有de ...

  6. vscode/webstorm快捷键

    ctrl+/  单行 [alt+shift+A]  多行注释 (默认的  我已经修改了) 复制当前行:shift + alt +up/down(上下箭头)可以修改成ctrl+d(改成webstorm一 ...

  7. javaScript核心基础

    JavaScript 是属于网络的脚本语言! JavaScript 作用:被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies(js也可创建cookie,在浏览器里面创建),以及更多 ...

  8. Python 操作 MySQL 的5种方式(转)

    Python 操作 MySQL 的5种方式 不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Pytho ...

  9. nuxtjs中修改head及vuex的使用

    1.在之前vue项目中,我们如果需要改变每个页面的title,是需要在路由里配置meta然后通过路由守卫将每个页面的title替换掉,但是在nuxtjs中他提供了一个方法,直接在每个.vue的文件中使 ...

  10. 如何在java List中进行模糊查询

    比如我有下面这样一个List,里面存放的是多个Employee对象.然后我想对这个List进行按照Employee对象的名字进行模糊查询.有什么好的解决方案么? 比如我输入的查询条件为“wang”,那 ...