文章作者: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
- https://leetcode.com/problems/reveal-cards-in-increasing-order/