We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents f201a57 + 894f2bb commit 89a0029Copy full SHA for 89a0029
swift/0228-summary-ranges.swift
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ func summaryRanges(_ nums: [Int]) -> [String] {
3
+ if nums.isEmpty {
4
+ return []
5
+ }
6
+ var l = 0
7
+ var res = [String]()
8
+ for r in 1..<nums.count {
9
+ if nums[r] != nums[r - 1] + 1 {
10
+ if l == r - 1 {
11
+ res.append("\(nums[l])")
12
+ } else {
13
+ res.append("\(nums[l])->\(nums[r - 1])")
14
15
+ l = r
16
17
18
+
19
+ if l == nums.count - 1 {
20
21
22
+ res.append("\(nums[l])->\(nums[nums.count - 1])")
23
24
25
+ return res
26
27
+}
0 commit comments