[Swift]LeetCode163. 缺失区间 $ Missing Ranges

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

[Swift]LeetCode163. 缺失区间 $ Missing Ranges的相关文章

[Swift]LeetCode268. 缺失数字 | Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear runtime

[LeetCode] Missing Ranges 缺失区间

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”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

[LeetCode] 163. Missing Ranges 缺失区间

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,Output: ["2", "4->49", "51-

[Swift中错误]missing argument label &#39;greeting:&#39; in call

Swift 中出现这个问题:从第二个参数起,自动加上lable func sayHello(name:String? ,greeting:String)->String { let result = greeting + "," + (name ?? "Guest") + "!" return result } var nickname:String? nickname = "yc" //“Goodmorning前面应该

leetcode[163]Missing Ranges

Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76-

[LeetCode] Missing Ranges

Problem Description: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51-&g

[Locked] Missing Ranges

Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74&q

163. Missing Ranges

题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "

Missing Ranges 解答

Question Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51-&