[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 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.
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 <= A.length <= 10001 <= A[i] <= 10^6A[i] != A[j]for alli != j.
Related Topics:
Array
Solution
题目要求找到一种序列,按照“取走一张→将牌堆顶的一张牌放到牌堆底”如此操作到牌堆无牌,使得这一叠牌按升序排列。解决方法是逆向推理:对于一个有序牌堆,从最后一张牌开始,将其从牌堆底取出,放到牌堆顶,然后将一张新牌放入牌堆顶,如此往复。
public class Solution
{
public int[] DeckRevealedIncreasing(int[] deck)
{
Array.Sort(deck);
LinkedList<int> originDeck = new LinkedList<int>();
originDeck.AddFirst(deck.Last());
for(int i = deck.Length - 2; i >= 0; i--)
{
int x = originDeck.Last.Value;
originDeck.RemoveLast();
originDeck.AddFirst(x);
originDeck.AddFirst(deck[i]);
}
return originDeck.ToArray();
}
}
[Solution] 950. Reveal Cards In Increasing Order的更多相关文章
- 【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 ...
- 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- [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. ...
- 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. ...
- 揭示牌面使之升序 Reveal Cards In Increasing Order
2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...
- Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌
牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
随机推荐
- LVS DR模式搭建、keepalived+lvs
1.LVS DR模式搭建 条件: 即三台机器,在同一内网. 编辑脚本文件:/usr/local/sbin/lvs_dr.sh #! /bin/bashecho 1 > /proc/sys/net ...
- java库中具体集合
ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行高效地插入和删除操作的有序序列 ArrayDeque 一种用循环数组实现的双端队列 HashSet ...
- 数组排序自定义comparator()
案例1:现在有一个普通数组arr = [3,1,2,4,5,6,8,0,1]; 自定义一个排序方法: function createComparator(){ return function (obj ...
- Java_初入IO流_字符流_Write-Read_小笔记
package IO; import java.io.FileWriter; import java.io.IOException; class File_Writer { public static ...
- linux kernel driver debug
1. print printk(): never pr_debug(): always good dev_dbg(): prefered when you have a struct device o ...
- grep 使用
如果需要搜索含空格的字符串,加双引号 | grep "cd /tmp" 参考链接 1.grep 后加单引号.双引号和不加引号的区别
- 十三、springboot (八)Admin
1.创建server端spring-boot-admin 2.添加依赖 <parent> <groupId>org.springframework.boot</group ...
- Springboot 允许跨域访问
服务提供段Application.java中添加如下代码: @Beanpublic CorsFilter corsFilter() { UrlBasedCorsConfigurationSource ...
- Excel技巧--按内容分列与合并
上表的A列,如果想要按横线分隔开多列,复制粘贴很麻烦,可以使用“数据”-->“分列”来分隔开: 1.选择A列,在A列后预先插入三列空列.点击“数据”—>“分列”,对话框选择按“分隔符号”分 ...
- 秒懂,Java 注解 (Annotation)你可以这样学 - CSDN博客
https://blog.csdn.net/briblue/article/details/73824058 文章开头先引入一处图片. 这处图片引自老罗的博客.为了避免不必要的麻烦,首先声明我个人比较 ...