Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f201a57 commit 894f2bbCopy full SHA for 894f2bb
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