Leetcode 950. Reveal Cards In Increasing Order

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

1. Description

Reveal Cards In Increasing Order

2. Solution

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

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
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
1
2
3
4
5
6
7
8
9
10
11
12
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/
如果有收获,可以请我喝杯咖啡!