Leetcode 950. Reveal Cards In Increasing Order

2021-07-01 10:30:21 浏览数 (1)

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

2. Solution

解析:按照题目规定的顺序给牌赋值即可。

  • Version 1
代码语言:javascript复制
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
代码语言:javascript复制
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

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

0 人点赞