题解 | 01序列
01序列
https://www.nowcoder.com/practice/b0c948dbe577485598b982a430d65c39
m = int(input())
list1 = list(map(int, input().split()))
n = int(input())
count = 0
i = 0
while i < m:
if list1[i] == 0:
j = i + 1
while j < m and list1[j] == 0:
j += 1
# 判断这段 0 是不是在边界
left_empty = (i == 0)
right_empty = (j == m)
length = j - i
if left_empty or right_empty:
# 在边上:(length + 1) // 2
cnt = (length + 1) // 2
else:
# 在中间被1夹住:(length - 1) // 2
cnt = (length - 1) // 2
count += cnt
i = j + 1
else:
i += 1
print('true' if count >= n else 'false')
查看13道真题和解析
