[Swift]LeetCode163. 缺失区间 $ Missing Ranges
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10126891.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 }