class Solution(object):
def constructArray(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[int]
"""
# 定义原始的列表,此时差值只有1种,均为1
res = list(range(1,n+1))
# 每次翻转1-n的列表,此时就会增加一种差值,第一个和最后一个
# 翻转的元素是-1到i
for i in range(1,k):
res[i:] = res[:i-1:-1]
return res
每日一道Leetcode - 667. 优美的排列 II
最新推荐文章于 2021-12-17 15:07:32 发布