Q2: Print missing numbers
Input: [2, 4, 7, 13, 20]
Output: "0,1,3,5,6,8-12,14-19,21-99"
Given a sorted list of numbers, print all missing numbers in the range from 0 to 99. Consecutive missing values should be compressed into ranges.
这道题的核心是遍历已排序数组,找出 <code>0</code> 到 <code>99</code> 之间所有未出现的数字,并把连续缺失的部分合并成区间输出。做法通常是先处理数组开头到第一个元素之前的缺失段,再逐个比较相邻元素之间的差值,最后补上末尾到 <code>99</code> 的缺失范围。这个题重点考察数组扫描、边界处理和区间格式化,属于很典型的 OA 字符串 / 数组输出题。
正文完