Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.
For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”]
给定一个排序的整数数数组,其中元素的范围包含[0,99],返回其缺少的范围。
例如,给定[0,1,3,50,75],返回[“2”,“4->49”,“51->74”,“76->99”]
1 class Solution { 2 func findMissingRanges(_ nums:[Int] ,_ lower:Int,_ upper:Int) -> [String]{ 3 var res:[String] = [String]() 4 var l:Int = lower 5 for i in 0...nums.count 6 { 7 var r:Int = (i < nums.count && nums[i] <= upper) ? nums[i] : upper + 1 8 if l == r 9 { 10 l += 1 11 } 12 else if r > l 13 { 14 res.append(r - l == 1 ? String(l):String(l) + "->" + String(r - 1)) 15 l = r + 1 16 } 17 } 18 return res 19 } 20 }
原文地址:https://www.cnblogs.com/strengthen/p/10126891.html
时间: 2024-10-03 21:13:45