|
| 1 | +/// You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, |
| 2 | +/// and two integers m and n, representing the number of elements in nums1 and nums2 respectively. |
| 3 | +/// |
| 4 | +/// Merge nums1 and nums2 into a single array sorted in non-decreasing order. |
| 5 | +/// |
| 6 | +// The final sorted array should not be returned by the function, |
| 7 | +/// but instead be stored inside the array nums1. |
| 8 | +/// To accommodate this, nums1 has a length of m + n, |
| 9 | +/// where the first m elements denote the elements that should be merged, |
| 10 | +/// and the last n elements are set to 0 and should be ignored. nums2 has a length of n. |
| 11 | +/// |
| 12 | +/// Example 1: |
| 13 | +/// |
| 14 | +/// Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 |
| 15 | +/// Output: [1,2,2,3,5,6] |
| 16 | +/// Explanation: The arrays we are merging are [1,2,3] and [2,5,6]. |
| 17 | +/// The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1. |
| 18 | +/// Example 2: |
| 19 | +/// |
| 20 | +/// Input: nums1 = [1], m = 1, nums2 = [], n = 0 |
| 21 | +/// Output: [1] |
| 22 | +/// Explanation: The arrays we are merging are [1] and []. |
| 23 | +/// The result of the merge is [1]. |
| 24 | +/// Example 3: |
| 25 | +/// |
| 26 | +/// Input: nums1 = [0], m = 0, nums2 = [1], n = 1 |
| 27 | +/// Output: [1] |
| 28 | +/// Explanation: The arrays we are merging are [] and [1]. |
| 29 | +/// The result of the merge is [1]. |
| 30 | +/// Note that because m = 0, there are no elements in nums1. |
| 31 | +/// The 0 is only there to ensure the merge result can fit in nums1. |
| 32 | +/// |
| 33 | +/// Constraints: |
| 34 | +/// |
| 35 | +/// nums1.length == m + n |
| 36 | +/// nums2.length == n |
| 37 | +/// 0 <= m, n <= 200 |
| 38 | +/// 1 <= m + n <= 200 |
| 39 | +/// -109 <= nums1[i], nums2[j] <= 109 |
| 40 | +
|
| 41 | +/// time O(m + n) |
| 42 | +/// space O(m + n) |
| 43 | +pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) { |
| 44 | + let m = m as usize; |
| 45 | + let n = n as usize; |
| 46 | + |
| 47 | + let mut result = Vec::with_capacity(m + n); |
| 48 | + |
| 49 | + let mut i: usize = 0; |
| 50 | + let mut j: usize = 0; |
| 51 | + |
| 52 | + loop { |
| 53 | + let maybe_num1 = if i < m { nums1.get(i) } else { None }; |
| 54 | + |
| 55 | + let min = match (maybe_num1, nums2.get(j)) { |
| 56 | + (None, None) => break, |
| 57 | + (Some(v), None) => { |
| 58 | + i += 1; |
| 59 | + v |
| 60 | + } |
| 61 | + (None, Some(v)) => { |
| 62 | + j += 1; |
| 63 | + v |
| 64 | + } |
| 65 | + (Some(a), Some(b)) => { |
| 66 | + if a <= b { |
| 67 | + i += 1; |
| 68 | + a |
| 69 | + } else { |
| 70 | + j += 1; |
| 71 | + b |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + result.push(*min); |
| 77 | + } |
| 78 | + |
| 79 | + let _ = std::mem::replace(nums1, result); |
| 80 | +} |
| 81 | + |
| 82 | +fn main() {} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_merge() { |
| 90 | + let cases = [ |
| 91 | + ( |
| 92 | + vec![1, 2, 3, 0, 0, 0], |
| 93 | + 3, |
| 94 | + vec![2, 5, 6], |
| 95 | + 3, |
| 96 | + vec![1, 2, 2, 3, 5, 6], |
| 97 | + ), |
| 98 | + (vec![1], 1, vec![], 0, vec![1]), |
| 99 | + (vec![0], 0, vec![1], 1, vec![1]), |
| 100 | + ]; |
| 101 | + |
| 102 | + for (mut nums1, m, mut nums2, n, expected) in cases { |
| 103 | + merge(&mut nums1, m, &mut nums2, n); |
| 104 | + assert_eq!(expected, nums1); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments