★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-moetlfmd-mc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a sorted integer array without duplicates, return the summary of its ranges.git
Example 1:github
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:app
輸入: [0,1,2,4,5,7] 輸出: ["0->2","4->5","7"] 解釋: 0,1,2 可組成一個連續的區間; 4,5 可組成一個連續的區間。
示例 2:spa
輸入: [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 }
8mscode
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 }
12msorm
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 }
12mshtm
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 }