题解 | #序列找数#
序列找数
https://www.nowcoder.com/practice/a7d1856a72404ea69fdfb5786d65539c
import sys
nums = set()
for line in sys.stdin:
ins = line.split()
[nums.add(int(num)) for num in ins]
l = len(nums)
pre = nums.pop()
if pre == 0:
for i in range(l-1):
cur = nums.pop()
if pre + 1 == cur:
pre = cur
continue
else:
print(pre + 1)
break
else:
print(0)
看题目要求,序列是从0开始的,也就需要对初始值进行判断。
本次解题利用set的有序不重复的特性实现插入式排序。
再通过set首删的函数特点,利用前指针进行连续性判断,连续则不为中断位置,不连续则为前指针数值的后一位为中断位置。
