Leetcode 950. Reveal Cards In Increasing Order
Posted SnailTyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 950. Reveal Cards In Increasing Order相关的知识,希望对你有一定的参考价值。
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
解析:按照题目规定的顺序给牌赋值即可。
- Version 1
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
deck.sort()
n = len(deck)
indexes = [i for i in range(n)]
result = [0] * n
for i in range(n):
index = indexes.pop(0)
result[index] = deck[i]
if indexes:
indexes.append(indexes.pop(0))
return result
- Version 2
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
deck.sort()
n = len(deck)
result = [0] * n
indexes = collections.deque([i for i in range(n)])
for i in range(n):
index = indexes.popleft()
result[index] = deck[i]
if indexes:
indexes.append(indexes.popleft())
return result
Reference
以上是关于Leetcode 950. Reveal Cards In Increasing Order的主要内容,如果未能解决你的问题,请参考以下文章
950. Reveal Cards In Increasing Order
950. Reveal Cards In Increasing Order
113th LeetCode Weekly Contest Reveal Cards In Increasing Order