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 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
随机推荐
- linux zip,tar压缩文件夹 忽略 .git 文件夾
linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...
- [JZOJ3339]【NOI2013模拟】wyl8899和法法塔的游戏
题目 题目大意 给你一个数列,每次给出\(r,a,b\),你要找到\(l\in [a,b]\)使得\([l,r-1]\)的异或和最小, 并且要修改\(r\)位置的数. 思考历程 当我看到这题的时候,已 ...
- sql语句之分组
对聚合函数的结果进行筛选用having,不能用where
- CSS——优雅降级和渐进增强
什么是渐进增强(progressive enhancement).优雅降级(graceful degradation)呢? 渐进增强 progressive enhancement: 针对低版本浏览器 ...
- BCB编写DLL终极手册
一. 编写 DLL File/New/Dll 生成 Dll 的向导,然后能够添加导出函数和导出类 导出函数:extern "C" __declspec(dllexport) Exp ...
- react diff 极简版
为什么react这么快呢 ? 因为react用了虚拟DOM: 但是每次虚拟DOM转真实DOM不也是很浪费性能吗 ? nice,所以关键点在Diff算法这里,去对比新旧DOM树,而后通过补丁去更新到真实 ...
- centos一些故障解决方法
1. vmware下虚拟机centos,root登录时候提示鉴定故障解决方法 - lippor - 博客园 https://www.cnblogs.com/lippor/p/5537931.html ...
- ps快速将白底图片变为透明图片
方法一: 如果图层有锁图标,则要点击它,然它消失.然后选中魔棒工具,然后点击图片上要透明的区域,按下backspace键即可. 方法二: 转载自:https://blog.csdn.net/sunyi ...
- 网络安全系列 之 SQL注入学习总结
目录 1. sql注入概述 2. sql注入测试工具 3. sql注入防御方法 3.1 问题来源 3.2 防御方法 4. SQL注入防御举例 4.1 使用JDBC时,SQL语句进行了拼接 4.2 使用 ...
- 18.scrapy_maitian
ershoufang.py # -*- coding: utf-8 -*- import scrapy class ErshoufangSpider(scrapy.Spider): name = 'e ...