【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 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
解题思路:这个题目有点意思。我们可以用输出来反推输入,输出可以看成是一个队列,记为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
【leedcode】950. Reveal Cards In Increasing Order的更多相关文章
- 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- [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 ...
- [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按递增顺序显示卡牌
牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...
- 【CF815D】Karen and Cards 单调栈+扫描线
[CF815D]Karen and Cards 题意:一张卡片有三个属性a,b,c,其上限分别为A,B,C,现在有n张卡片,定义一张卡片能打败另一张卡片当且仅当它的至少两项属性要严格大于另一张的对应属 ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
随机推荐
- Python格式输出汇总
print ('%10s'%('test')) print ('{:<10}'.format('test'))#left-aligned print ('{:>10}'.format('t ...
- springMVC带参数请求重定向
SpirngMVC返回逻辑视图名 可以分下面几种情况: 1. servlet进行请求转发,返回到jsp页面,如 return "index.jsp" ; 2. servlet 返 ...
- 组合的输出(回溯、dfs)
问题 O: [回溯法]组合的输出 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r ...
- python 简单抓取网页并写入excel实例
# -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...
- bugku | 你从哪里来
题目链接 之前一直以为要用x-forwarded-for ,谁道用的是referer,Orz.在此特地记录,x-forwarded-for 和 referer的区别 X-Forwarded-For(X ...
- [CSP-S模拟测试]:Revive(点分治)
题目背景 $Sparkling\ ashes\ drift\ along\ your\ flames \\ And\ softly\ merge\ into\ the\ sky$ 题目传送门(内部题1 ...
- MySQL-5.7填坑
绿色版(zip archive 版)无 my-default.ini As of MySQL 5.7.18, my-default.ini is no longer included in or in ...
- selenium2-java 浏览器cookie的获取
//成功登陆后增加如下代码 File cookieFile = new File("C:\\tmp\\tangdai.cookie.txt"); ...
- T1373:鱼塘钓鱼(fishing)
原题链接:1373:鱼塘钓鱼(fishing) 解题思路: 由于在走路时,鱼的数量不会减少,那我们此时可以先减去路上可能花掉的时间,用剩下的时间来找最多的鱼,然后从左向右走,k枚举能到达的最远的鱼塘, ...
- python开发必备pycharm专业版破解方法
修改hosts文件 添加下面一行到hosts文件,目的是屏蔽掉Pycharm对激活码的验证 0.0.0.0 account.jetbrains.com 注:hosts文件路径,Windows在C:\W ...