MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / summary_ranges

Function summary_ranges

228-summary-ranges.rs:1–28  ·  view source on GitHub ↗
(nums: Vec<i32>)

Source from the content-addressed store, hash-verified

1pub fn summary_ranges(nums: Vec<i32>) -> Vec<String> {
2 let mut ret = vec!();
3 if nums.len() == 0 {
4 return ret;
5 }
6 pub fn assemble(current: Vec<i32>) -> String {
7 if current.len() == 1 {
8 current[0].to_string()
9 } else {
10 let f = current.first().unwrap().to_string();
11 let l = current.last().unwrap().to_string();
12 vec!(f, "->".to_string(), l).join("")
13 }
14 }
15 let mut prev = nums[0];
16 let mut current = vec!(prev);
17 for num in &nums[1..] {
18 if num - prev != 1 {
19 ret.push(assemble(current));
20 current = vec!(*num);
21 } else {
22 current.push(*num);
23 }
24 prev = *num;
25 }
26 ret.push(assemble(current));
27 ret
28}
29
30fn main() {
31 let nums = vec![];

Callers

nothing calls this directly

Calls 2

assembleFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected