题目如下:

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:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
  3. 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. 1 <= A.length <= 1000
  2. 1 <= A[i] <= 10^6
  3. A[i] != A[j] for all i != j

解题思路:这个题目有点意思。我们可以用输出来反推输入,输出可以看成是一个队列,记为queue,最后入队的是最大值,最先入队的是最小值。从输入到输出的逻辑是先把deck的第一个元素放入queue,然后第二个元素放入deck的最后;那么从输出反推的输入的逻辑就是先把deck的最后一个元素放入到deck的第一个位置,然后再把queue的最后一个元素放入deck。

代码如下:

class Solution(object):
def deckRevealedIncreasing(self, deck):
"""
:type deck: List[int]
:rtype: List[int]
"""
deck.sort(reverse = True)
res = []
while len(deck) > 0:
v = deck.pop(0)
if len(res) == 0:
res.append(v)
else:
res.insert(0,res.pop(-1))
res.insert(0,v)
return res

最新文章

  1. linux指令学习笔记(一)
  2. 从业十余年谈谈对dotnet看法与坚持
  3. 设置时间 new Date
  4. 在Linq to Entity 中使用lambda表达式来实现Left Join和Join
  5. ThinkPHP CURD方法盘点:limit方法
  6. Ubuntu下配置Scheme开发环境
  7. poj1064 二分,注意精度!
  8. doGet与doPost的区别
  9. nginx源代码分析--模块分类
  10. Basic Sort Algorithms
  11. JS进阶 ] 分析JS中的异步操作
  12. 【转】构建高性能WEB站点之 吞吐率、吞吐量、TPS、性能测试
  13. (暴力求解)Encoding HDU1020
  14. Android获取本机号码及运营商
  15. 三 、 Multivariance Linear Regssion练习(转载)
  16. 在sourceinsight中添加快速注释 Ctrl+/
  17. 在Linux中以普通用户开机自动运行脚本程序
  18. 066——VUE中vue-router之rewrite模式下配置404页面
  19. 仿饿了吗点餐界面两个ListView联动效果
  20. eclipse安装checkStyle

热门文章

  1. 使用kindeditor直接粘贴本地图片或者是qq截图
  2. IGServer for Java
  3. Delphi Base64编码/解码
  4. Spring JDK动态代理
  5. nboot,eboot和uboot
  6. JS - 模块
  7. android SlidingDrawer
  8. 18. Jmeter-取样器二
  9. Django token 学前小知识
  10. sublime text3中使用PHP编译系统