[Swift]LeetCode228. 汇总区间 | Summary Ranges

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10204442.html

时间: 01-01

[Swift]LeetCode228. 汇总区间 | Summary Ranges的相关文章

[LeetCode]228.Summary Ranges

题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 代码 /*--------------------------------------- * 日期:2015-08-04 * 作者:SJF01

[LeetCode] Summary Ranges 总结区间

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits:Special thanks to @jianchao.li.fighter for adding this problem and

LeetCode OJ:Summary Ranges(概括区间)

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 就是概括区间的方式把一个数组表示下来,例如123用1->3代替,自己写的很乱 ,维护两个指针就可以了: 1 class Solution {

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits: Special thanks to @jianchao.li.fighter for add

Summary Ranges —— LeetCode

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 题目大意:给一个有序数组,返回连续的区间范围. public class Solution { public List<String>

LeetCode -- Summary Ranges

题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits:Special thanks to @jianchao.li.fighter for adding this probl

LeetCode——Summary Ranges

Description: Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 确定有序数组的区间 需要注意各种边界细节,每次确定区间之后需要清空. public class Solution { pu

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 这个题目不难,首先对原序列进行排序,然后要注意对特殊情况的处理.同时,要掌握string类的几个重要成员函数: to_string():将数字转换为

Java for LeetCode 228 Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 解题思路: JAVA实现如下: public List<String> summaryRanges(int[] nums) { List

Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Runtime: 0ms 1 class Solution { 2 public: 3 vector<string> summaryRa