Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌
牌组中的每张卡牌都对应有一个唯一的整数。你可以按你想要的顺序对这套卡片进行排序。
最初,这些卡牌在牌组里是正面朝下的(即,未显示状态)。
现在,重复执行以下步骤,直到显示所有卡牌为止:
- 从牌组顶部抽一张牌,显示它,然后将其从牌组中移出。
- 如果牌组中仍有牌,则将下一张处于牌组顶部的牌放在牌组的底部。
- 如果仍有未显示的牌,那么返回步骤 1。否则,停止行动。
返回能以递增顺序显示卡牌的牌组顺序。
答案中的第一张牌被认为处于牌堆顶部。
示例:
输入:[17,13,11,2,3,5,7] 输出:[2,13,3,11,5,17,7] 解释: 我们得到的牌组顺序为 [17,13,11,2,3,5,7](这个顺序不重要),然后将其重新排序。 重新排序后,牌组以 [2,13,3,11,5,17,7] 开始,其中 2 位于牌组的顶部。 我们显示 2,然后将 13 移到底部。牌组现在是 [3,11,5,17,7,13]。 我们显示 3,并将 11 移到底部。牌组现在是 [5,17,7,13,11]。 我们显示 5,然后将 17 移到底部。牌组现在是 [7,13,11,17]。 我们显示 7,并将 13 移到底部。牌组现在是 [11,17,13]。 我们显示 11,然后将 17 移到底部。牌组现在是 [13,17]。 我们展示 13,然后将 17 移到底部。牌组现在是 [17]。 我们显示 17。 由于所有卡片都是按递增顺序排列显示的,所以答案是正确的。
提示:
- 1 <= A.length <= 1000
- 1 <= A[i] <= 10^6
- 对于所有的 i != j,A[i] != A[j]
需要对一组线性数据的头尾不断进行操作,所以可以巧妙使用双端队列。
bool cmp1(int x, int y)
{
return x < y;
}
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck)
{
int len = deck.size();
sort(deck.begin(), deck.end(), cmp1);
deque<int> dq;
vector<int> Ans;
int i = len - 1;
while(i >= 0)
{
if(!dq.empty())
{
int temp = dq.back();
dq.pop_back();
dq.push_front(temp);
}
dq.push_front(deck[i]);
i--;
}
Ans.assign(dq.begin(), dq.end());
return Ans;
}
};
Leetcode950. 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. ...
- leetcode《按递增顺序显示卡牌》
题目描述: 牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从 ...
- 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. ...
- [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 ...
- 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 ...
- 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- 揭示牌面使之升序 Reveal Cards In Increasing Order
2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
随机推荐
- Android开发 navigation的跳转动画实现
前言 此篇博客只简短的介绍navigation如何添加跳转页面的动画属性,如果你还为接触了解过navigation.建议你看我另一篇博客Android开发 navigation入门详解 创建动画xml ...
- virtualbox manager命令小记
virtualbox 控制虚拟机 VBoxManage list runningvms 列出运行的虚拟机 (返回名称和UUID): VBoxManage list runningvms Stop ru ...
- JAVA数据结构之红-黑树
本篇博客我会重点介绍对红-黑树的理解,重点介绍红-黑树的查找,这里我们将要讨论的算法称为自顶向下插入,也就是把沿着树向下查找插入点 Ⅰ.平衡树和非平衡树 平衡树和非平衡树:当插入一组数据关键字是按照升 ...
- Cut the Sequence
Cut the Sequence 有一个长度为n的序列\(\{a_i\}\),现在求将其划分成若干个区间,并保证每个区间的和不超过m的情况下,每个区间的最大值的和的最小值,\(0 < N ≤ 1 ...
- Java 多线程 - 锁的类型
https://zhuanlan.zhihu.com/p/37287566 https://www.cnblogs.com/qifengshi/p/6831055.html
- Windows where
WHERE [/R dir] [/Q] [/F] [/T] pattern... 描述: 显示符合搜索模式的文件位置.在默认情况下,搜索是在当前目录和 PATH 环境变量指定的路径中执行的 ...
- day31 类的组合及继承,文件目录规范
Python之路,Day18 = Python基础18-面向对象继承与组合 类的继承 def talk(): print("I am come from talk..a") cla ...
- 关于Ubuntu锁屏后,无法输入密码
屏幕锁定后字母密码无法输入,数字可以,切换用户然后可以正常输入密码登陆. 是输入法的事,锁屏界面依然是中文输入法,然后输入字母之后先不会显示然后加上数字后是汉字密码不对. 切换输入法到英文,就可以正常 ...
- 9.1 mongo_python.py
# 安装 pymongo pip install pymongo import pymongo try: # 1.链接mongod的服务 mongo_py = pymongo.MongoClient( ...
- javaSpring学习总结day_02
使用注解注入: 1.用于创建bean对象 @Component: 作用:相当于配置了一个bean标签 位置:类上面 属性:value,含义是bean的id,当不写时,有默认值,默认值是当前类的短名,首 ...