20240401 Finished

This commit is contained in:
jackfiled 2024-04-01 11:23:10 +08:00
parent e839fe55e2
commit 174a17f94c
2 changed files with 67 additions and 1 deletions

View File

@ -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 p1997_first_day_where_you_have_been_in_all_the_rooms;
mod p2908_minimum_sum_of_mountain_triplets_i; mod p2908_minimum_sum_of_mountain_triplets_i;
mod p2952_minimum_number_of_coins_to_be_added; mod p2952_minimum_number_of_coins_to_be_added;
mod p331_verify_preorder_serialization_of_a_binary_tree; mod p331_verify_preorder_serialization_of_a_binary_tree;
mod p88_merge_sorted_array;

View File

@ -0,0 +1,65 @@
/**
* [88] Merge Sorted Array
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, 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]);
}
}