-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructArray.py
More file actions
38 lines (31 loc) · 768 Bytes
/
constructArray.py
File metadata and controls
38 lines (31 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 667. 优美的排列 2
from typing import List
class Solution:
# def constructArray(self, n: int, k: int) -> List[int]:
# res = list(range(1, n-k))
# i, j = n - k, n
# while i <= j:
# res.append(i)
# if i != j:
# res.append(j)
# i, j = i + 1, j - 1
# return res
def constructArray(self, n: int, k: int) -> List[int]:
res = list(range(1, n-k))
i, j = n - k, n
while i <= j:
res.append(i)
if i != j:
res.append(j)
i, j = i + 1, j - 1
return res
s = Solution()
n = 3
k = 1
print(s.constructArray(n, k))
n = 3
k = 2
print(s.constructArray(n, k))
n = 6
k = 5
print(s.constructArray(n, k))