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. PHP面试大全 基础篇100道问题

    2017年3月7日14:23:21 其实计划很久但是移植没时间去收集和处理弄成一个完整的文件 SVN地址: https://git.oschina.net/zxadmin/live_z 目前基础部分更 ...

  2. Luogu 1031 - 均分纸牌 - [有意思的思维题]

    题目链接:https://www.luogu.org/problemnew/show/P1031 题目描述有 $N$ 堆纸牌,编号分别为 $1,2,…,N$.每堆上有若干张,但纸牌总数必为 $N$ 的 ...

  3. ExceptionLess使用

    1.os使用windows server 2012,省的升级iis express-iis 8(测试环境是IIS Express 8,生产环境IIS 7.5).PowerShell 3+了. 2.wi ...

  4. 20144306《网络对抗》MAL_恶意代码分析

    一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控? 使用Windows自带的schtasks指 ...

  5. LaTeX大于小于号

    发现大部分人只回答大于等于号.小于等于号的写法,而没有说大于.小于号的分别写法. 大于号:\textgreater 小于号: \textless 下面的后面要加空格,否则会识别错误 大于等于:\geq ...

  6. java框架之Shiro-安全/权限框架

    准备 简介 Apache Shiro 是 Java 的一个安全(权限)框架. Shiro 不仅可以用在 JavaSE 环境,也可以用在 JavaEE 环境. Shiro 可以完成:认证.授权.加密.会 ...

  7. vue 自适应 Responsive 设计

    使用阿里的 lib-flexible 及 vue-meta 库: https://github.com/amfe/lib-flexible/ https://github.com/nuxt/vue-m ...

  8. export to excel

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel;npoi是重点. 定义一个exporttoe ...

  9. (转载)intellj idea 如何设置类头注释和方法注释

    原文地址:http://www.cnblogs.com/wvqusrtg/p/5459327.html           intellj idea的强大之处就不多说了,相信每个用过它的人都会体会到, ...

  10. JAVA:IDEA安装、创建项目、tomcat配置、maven配置(1)

    一.安装 JDK安装自行百度,IDEA安装: IDEA安装包下载:https://www.jetbrains.com/idea/download/ 安装过程一路默认,需要选择的就勾选 .java之类的 ...