diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 8b85ce2..4ccf9be 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -90,4 +90,5 @@ mod p2580_count_ways_to_group_overlapping_ranges; mod p1997_first_day_where_you_have_been_in_all_the_rooms; mod p2908_minimum_sum_of_mountain_triplets_i; mod p2952_minimum_number_of_coins_to_be_added; -mod p331_verify_preorder_serialization_of_a_binary_tree; \ No newline at end of file +mod p331_verify_preorder_serialization_of_a_binary_tree; +mod p88_merge_sorted_array; \ No newline at end of file diff --git a/src/problem/p88_merge_sorted_array.rs b/src/problem/p88_merge_sorted_array.rs new file mode 100644 index 0000000..cf9533c --- /dev/null +++ b/src/problem/p88_merge_sorted_array.rs @@ -0,0 +1,65 @@ +/** + * [88] Merge Sorted Array + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn merge(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { + let (m, n) = (m as usize, n as usize); + let mut result = Vec::with_capacity(m + n); + + let (mut i, mut j) = (0, 0); + + while i < m || j < n { + if i == m { + while j < n { + result.push(nums2[j]); + j += 1; + } + + break; + } + + if j == n { + while i < m { + result.push(nums1[i]); + i += 1; + } + + break; + } + + + if nums1[i] < nums2[j] { + result.push(nums1[i]); + i += 1; + } else { + result.push(nums2[j]); + j += 1; + } + } + + for (index, &value) in result.iter().enumerate() { + nums1[index] = value; + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_88() { + let mut nums1 = vec![1]; + let mut nums2 = vec![]; + + Solution::merge(&mut nums1, 1, &mut nums2, 0); + assert_eq!(nums1, vec![1]); + } +}