Skip to content

Commit adfabe0

Browse files
committed
Sync LeetCode submission - Summary Ranges (rust)
1 parent 7dba398 commit adfabe0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

problems/summary_ranges/solution.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn summary_ranges(nums: Vec<i32>) -> Vec<String> {
3+
let mut ans = Vec::new();
4+
if nums.is_empty() {
5+
return Vec::new();
6+
}
7+
let mut current_range = (nums[0], nums[0]);
8+
9+
for num in &nums[1..] {
10+
if *num == current_range.1 + 1 {
11+
current_range.1 += 1;
12+
} else {
13+
if current_range.0 == current_range.1 {
14+
ans.push(format!("{}", current_range.0))
15+
} else {
16+
ans.push(format!("{}->{}", current_range.0, current_range.1));
17+
}
18+
current_range = (*num, *num);
19+
}
20+
}
21+
22+
if current_range.0 == current_range.1 {
23+
ans.push(format!("{}", current_range.0))
24+
} else {
25+
ans.push(format!("{}->{}", current_range.0, current_range.1));
26+
};
27+
28+
ans
29+
}
30+
31+
}

0 commit comments

Comments
 (0)