Skip to content

Commit 89a0029

Browse files
authoredDec 10, 2024
Merge pull request #3763 from drxlx/0228-summary-ranges
Create 0228-summary-ranges.swift
2 parents f201a57 + 894f2bb commit 89a0029

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

Diff for: ‎swift/0228-summary-ranges.swift

+27
Original file line numberDiff line numberDiff line change
@@ -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+
res.append("\(nums[l])")
21+
} else {
22+
res.append("\(nums[l])->\(nums[nums.count - 1])")
23+
}
24+
25+
return res
26+
}
27+
}

0 commit comments

Comments
 (0)