题目

链接:https://leetcode.com/problems/reveal-cards-in-increasing-order/

Level: Medium

Discription:

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 all i != j

代码

class Solution {
public:vector<int> deckRevealedIncreasing(vector<int>& deck) {sort(deck.begin(),deck.end());vector<int> temp=deck;for(int i=0; i<deck.size();i++){ int index = 2*i;while(index >=deck.size())index = (index%deck.size())*2+1;temp[index]=deck[i];  }return temp;}
};

思考

  • 算法时间复杂度为O(NlogN),空间复杂度为O(N)。
  • 找规律

转载于:https://www.cnblogs.com/zuotongbin/p/10214464.html

Leetcode 950. Reveal Cards In Increasing Order相关推荐

  1. leetcode950. Reveal Cards In Increasing Order

    题目链接 题目大意:桌子上有一副牌(a deck of cards),面朝下扣着,翻开第一张,然后拿走,并且把其后的一张牌放到这副牌的最下面,以此类推,直到把所有牌都翻开.求这副牌开始应该以什么顺序放 ...

  2. Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌

    牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...

  3. LeetCode 103. Binary Tree Zigzag Level Order Traversal

    LeetCode 103. Binary Tree Zigzag Level Order Traversal Solution1:基于层次遍历的微改 /*** Definition for a bin ...

  4. LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  5. LeetCode: 103. Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  6. [Leetcode]@python 107. Binary Tree Level Order Traversal II

    题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题目原文 Given a binary tree, r ...

  7. leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)

    参考 Python 解法: 动态规划 -- 最长递增子序列(LIS) 原题位置:Longest Increasing Subsequence | LeetCode OJ 题目的说明: 严格递增: 子序 ...

  8. leetcode103JAVA_[LeetCode] 103. Binary Tree Zigzag Level Order Traversal Java

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

  9. LeetCode 950. 按递增顺序显示卡牌(deque)

    文章目录 1. 题目 2. 解题 1. 题目 牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以 ...

最新文章

  1. 基因组组装----k-mer
  2. List集合的迭代器方法
  3. 全局样式_CAD新手福利:不懂标注样式修改的请进来一看
  4. Ubuntu 10.10升级显卡驱动后开机动画低分辨率问题
  5. leetcode461. 汉明距离
  6. 特征工程系列之降维:用PCA压缩数据
  7. 被指涉嫌“二选一” 山姆回应:欢迎良性竞争
  8. 组装台式电脑配置清单_萌新攒机必备!多价位台式电脑配置清单!
  9. 修改Imdict做自己的分词器
  10. c语言程序设计数字电位器,可编程数字电位器在AVR单片机中的应用
  11. hdoj2154跳舞毯
  12. 通过驱动断链来隐藏驱动
  13. 神舟笔记本WLAN打不开的解决方法
  14. 笔记本连接显示器后没有声音
  15. 中国反挖矿举措立功,欧洲英伟达显卡价格已大幅回落
  16. 天天向商为什么更名,更名为稿定设计之后有什么变化?
  17. GPU Raid卡加持!PBlaze6 6920挑战8盘Raid5
  18. 上半年要写的博客文章29
  19. Web站相关知识与Web程序的访问流程
  20. C语言实现总体方差,总体标准差,样本方差,样本标准差

热门文章

  1. jenkins安装(用户配置)(2)
  2. stylus之方法(Functions)
  3. (转)C结构体之位域(位段)
  4. python安装django模块_python中安装django模块的方法
  5. 云计算第二阶段shell脚本
  6. B. 重载技术(overloading)
  7. 【转载】安卓开发者在使用deepin15.4时可能会遇到的问题
  8. Shell入门教程:算术运算
  9. ios 在UIView上画图,线条
  10. PMP读书笔记(第4章)