/
main.swift
43 lines (37 loc) · 1.2 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//: Playground - noun: a place where people can play
class Solution {
func summaryRanges(_ nums: [Int]) -> [String] {
if nums.count <= 0 {
return [];
}
if nums.count <= 1{
return ["\(nums[0])"];
}
var result:[String] = [];
var leftValue = nums[0];
var rigthValue = nums[0];
for index in 1 ..< nums.count{
if nums[index] > nums[index - 1] + 1{
rigthValue = nums[index - 1];
if leftValue == rigthValue{
result.append("\(leftValue)");
}else{
result.append("\(leftValue)->\(rigthValue)");
}
leftValue = nums[index];
rigthValue = nums[index];
}
if(index == nums.count - 1){
rigthValue = nums[index];
if leftValue == rigthValue{
result.append("\(leftValue)");
}else{
result.append("\(leftValue)->\(rigthValue)");
}
}
}
return result;
}
}
let solution = Solution();
solution.summaryRanges([0]);