[Swift]LeetCode228. 汇总区间 | Summary Ranges
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10204442.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
示例 1:
输入: [0,1,2,4,5,7] 输出: ["0->2","4->5","7"] 解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。
示例 2:
输入: [0,2,3,4,6,8,9] 输出: ["0","2->4","6","8->9"] 解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
8ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 4 var resultArr = [String]() 5 var tempValue = 0 6 for (index ,value) in nums.enumerated() { 7 var tempStr = "\(value)" 8 if index == 0 { 9 tempValue = value 10 resultArr.append(tempStr) 11 }else{ 12 if value == nums[index - 1] + 1 { 13 tempStr = "\(tempValue)->\(value)" 14 resultArr.removeLast() 15 resultArr.append(tempStr) 16 }else{ 17 tempValue = value 18 resultArr.append(tempStr) 19 } 20 } 21 22 } 23 return resultArr 24 } 25 }
8ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 if nums.count < 1 { 4 return [] 5 } 6 var ans = [String]() 7 var first = nums[0] 8 var lastV = first 9 for i in 1..<nums.count { 10 let cv = nums[i] 11 if cv - lastV > 1 { 12 let str = (lastV == first) ? "\(first)" : "\(first)->\(lastV)" 13 ans.append(str) 14 first = cv 15 } 16 lastV = cv 17 } 18 let str = (lastV == first) ? "\(first)" : "\(first)->\(lastV)" 19 ans.append(str) 20 return ans 21 } 22 }
12ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 let n = nums.count 4 if n == 0 { 5 return [String]() 6 } 7 if n == 1 { 8 return ["\(nums[0])"] 9 } 10 var temp = [String]() 11 var start = 0 12 var i = 0 13 while i < n - 1 { 14 if nums[i] == nums[i + 1] - 1 { 15 i += 1 16 if i == n - 1 { 17 temp.append("\(nums[start])->\(nums[i])") 18 } 19 continue 20 } 21 if start == i { 22 temp.append("\(nums[start])") 23 } else { 24 temp.append("\(nums[start])->\(nums[i])") 25 } 26 i += 1 27 start = i 28 if i == n - 1 { 29 temp.append("\(nums[start])") 30 } 31 } 32 return temp 33 } 34 }
12ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 if nums.isEmpty { 4 return [] 5 } 6 var res = [String]() 7 8 var tmp = [nums[0]] 9 10 for i in 1..<nums.count { 11 let num = nums[i] 12 if num == tmp.last! + 1 { 13 tmp.append(num) 14 }else { 15 if tmp.count == 1 { 16 res.append("\(tmp[0])") 17 }else { 18 res.append("\(tmp[0])->\(tmp.last!)") 19 } 20 tmp.removeAll() 21 tmp.append(num) 22 } 23 } 24 if tmp.count == 1 { 25 res.append("\(tmp[0])") 26 }else { 27 res.append("\(tmp[0])->\(tmp.last!)") 28 } 29 30 return res 31 } 32 }
16ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 var result = [String]() 4 guard nums.count > 0 else { return result } 5 if nums.count == 1 { return ["\(nums[0])"] } 6 var start = nums[0] 7 var end = nums[0] 8 for i in 1 ..< nums.count { 9 let current = nums[i] 10 if current != end + 1 { 11 if start == end { 12 result.append("\(start)") 13 } else { 14 result.append("\(start)->\(end)") 15 } 16 start = current 17 end = current 18 } else { 19 end = current 20 } 21 } 22 if start == end { 23 result.append("\(start)") 24 } else { 25 result.append("\(start)->\(end)") 26 } 27 return result 28 } 29 }
104ms
1 class Solution { 2 func summaryRanges(_ nums: [Int]) -> [String] { 3 var res = [String]() 4 var str = "" 5 var start = 0 6 7 guard nums.count > 0 else { 8 return res 9 } 10 11 for i in 0 ... nums.count { 12 if i == nums.count || (i > 0 && nums[i] != nums[i - 1] + 1) { 13 str = "\(nums[start])" 14 if i - 1 != start { 15 str += "->\(nums[i - 1])" 16 } 17 res.append(str) 18 start = i 19 } 20 } 21 22 return res 23 } 24 }