揭示牌面使之升序 Reveal Cards In Increasing Order
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的更多相关文章
- [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. ...
- [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 ...
- 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- 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. ...
- 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. ...
- 【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 ...
- Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌
牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...
- 输入n,然后输入n个数,使它升序输出
#include<iostream> using namespace std; int main() { int n,i,j,m,k; cin>>n; int a[n]; f ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- shop_z 一套非常适合二次开发的php后台管理系统
QQ群:247823727 如果你需要定制某些功能开联系群主,价格实惠,后期交接完善,有上手培训 shop_z基础thinkphp5 php7上开发运行,速度杠杠的 地址:https://gitee ...
- Eclipse 00: 常用快捷键
Eclipse常用快捷键 1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索 ...
- Spark application注册master机制
直接上Master类的代码: case RegisterApplication(description) => { if (state == RecoveryState.STANDBY) { / ...
- java并发包消息队列(也即阻塞队列BlockingQueue)
下面是典型的消息队列的生产者与消费者模式的例子
- 【JVM】-NO.111.JVM.1 -【JDK11 HashMap详解-1-hash()剖析】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- metasploit安装,按官网说明
mkdir -p $HOME/git cd $HOME/git git clone git@github.com:YOUR_USERNAME_FOR_GITHUB/metasploit-framewo ...
- Gitlab+Jenkins实现自动部署
Gitlab+Jenkins实现自动部署 系统环境: Gitlab主机 IP:192.168.1.2 Jenkins主机 IP:192.168.1.3 一.为何要做自动部署 #部署Tomcat的在 ...
- HTTPS学习笔记一----HTTPS的基础理论知识
首先推荐一本书,<HTTP权威指南>我就是看这本书入门的,对http协议有了更好的理解,学习https的理论知识我认为需要了解以下几点,需要一步步的深入学习: 1.HTTPS的基本概念? ...
- 第七篇——Struts2的接收参数
Struts2的接收参数 1.使用Action的属性接收参数 2.使用Domain Model接收参数 3.使用ModelDriven接收参数 项目实例 1.项目结构 2.pom.xml <pr ...
- Hdu2039 三角形
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2039 三角形 Time Limit: 2000/1000 MS (Java/Others) Me ...