1。给个sorted array, 只包含不重复整数,数值范围在 [0,99]中间, 按格式打
印其余数值:
输入: [0,1,3,50,75]
输出: [2,4-49,51-74,76-99]
请写出程序,及 testing cases。
1 string findConsecutive(vector<int> nums) { 2 if (nums.empty()) return ""; 3 stringstream ss; 4 5 if (nums[0] > 1) { 6 ss << "0-" << nums[0] - 1; 7 } 8 9 for (int i = 1; i < nums.size(); ++i) { 10 if (nums[i] - nums[i - 1] > 1) { 11 ss << "," << (nums[i - 1] + 1) << "-" << (nums[i] - 1); 12 } 13 } 14 15 if (nums.size() > 1 && 99 - nums.back() > 1) { 16 ss << "," << nums.back() + 1 << "-99"; 17 } 18 return ss.tostring(); 19 }