merge two vectors to get the max number, use vector comparsion O(n) time
(x: Option<Vec<i32>>, y: Option<Vec<i32>>)
| 23 | /// merge two vectors to get the max number, use vector comparsion |
| 24 | /// O(n) time |
| 25 | pub fn merge(x: Option<Vec<i32>>, y: Option<Vec<i32>>) -> Vec<i32> { |
| 26 | match (x, y) { |
| 27 | (None, Some(x)) => return x, |
| 28 | (Some(x), None) => return x, |
| 29 | (Some(x), Some(y)) => { |
| 30 | let (n1, n2) = (x.len(), y.len()); |
| 31 | let mut res = vec![]; |
| 32 | let (mut i, mut j) = (0, 0); |
| 33 | loop { |
| 34 | if i == n1 { res.append(&mut Vec::from(&y[j..])); return res } |
| 35 | if j == n2 { res.append(&mut Vec::from(&x[i..])); return res } |
| 36 | if x[i] > y[j] { res.push(x[i]); i += 1 } |
| 37 | else if x[i] < y[j] { res.push(y[j]); j += 1 } |
| 38 | else /* x[i] == y[j] */ { |
| 39 | if &x[i..] > &y[j..] { res.push(x[i]); i += 1 } |
| 40 | else { res.push(y[j]); j += 1 } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | _ => unreachable!() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// calculate the max `digit_cnt` digit number of the vector `nums` |
| 49 | /// O(n) time |