[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 <= 1000
1 <= A[i] <= 10^6
A[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; 完
随机推荐
- message box
QMessageBox 弹出框上的按钮设置为中文 Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::War ...
- 在Linux中执行.sh脚本,异常
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...
- PHP 打开已有图片进行编辑
<?php header("content-type:image/jpeg");//表明请求页面的内容是jpeg格式的图像 即当前页面会以图片的新式展示 去掉这行就可以显示正 ...
- MyBatis举例以及连接数据库过程
第一:创建实体类 package entity; public class Emp { private int empno; private String ename; private String ...
- C# 利用反射完成计算器可扩展功能
一个主要的窗体程序,两个输入框,一个label using System; using System.Collections.Generic; using System.ComponentModel; ...
- SharpDevelope 在 Windows 7 SP1 with .net framework4.0 下编译时找不到resgen.exe 解决办法
如果在vs下编译正常,在SharpDevelope下编译报这个错误,可以更改编译时的.netframework版本和C#版本.在 Tool->Project Upgrade 进行项目转换后,一般 ...
- 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot
========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...
- Centos7.3 之mysql5.7二进制安装
#!/bin/bash #注意,该脚本是在centos7.3非生产环境下测试的,其他版本的系统可能不适用,要根据情况修改.需要先下载好mysql二进制包到本地(我一般都是在root家目录下操作,文件也 ...
- bootstrap 模态框事件
事件 描述 实例 show.bs.modal 在调用 show 方法后触发. $('#identifier').on('show.bs.modal', function () { // 执行一些动作. ...
- 图像小波变换去噪——MATLAB实现
clear; [A,map]=imread('C:\Users\wangd\Documents\MATLAB\1.jpg'); X=rgb2gray(A); %画出原始图像 subplot(,,);i ...