-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
37 lines (33 loc) · 944 Bytes
/
Solution.rs
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
impl Solution {
#[allow(dead_code)]
pub fn summary_ranges(nums: Vec<i32>) -> Vec<String> {
if nums.is_empty() {
return vec![];
}
let mut ret = Vec::new();
let mut start = nums[0];
let mut prev = nums[0];
let mut current = 0;
let n = nums.len();
for i in 1..n {
current = nums[i];
if current != prev + 1 {
if start == prev {
ret.push(start.to_string());
} else {
ret.push(start.to_string() + "->" + &prev.to_string());
}
start = current;
prev = current;
} else {
prev = current;
}
}
if start == prev {
ret.push(start.to_string());
} else {
ret.push(start.to_string() + "->" + &prev.to_string());
}
ret
}
}