-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sorted_array.rs
72 lines (64 loc) · 2.17 KB
/
merge_sorted_array.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// # Merge Sorted Array
// You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
//
// Merge `nums1` and `nums2` into a single array sorted in non-decreasing order.
//
// The final sorted array should **not** be returned by the function, but instead be stored inside the array `nums1`.
// To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
//
// ## Constraints:
// - `nums1.length == m + n`
// - `nums2.length == n`
// - `0 <= m, n <= 200`
// - `1 <= m + n <= 200`
// - `-109 <= nums1[i], nums2[j] <= 109`
pub fn solution(nums1: &mut [i32], m: i32, nums2: &mut [i32], n: i32) {
if n == 0 {
return;
}
let mut nums2_index = n;
let mut nums1_index = m;
let mut last_num_index = nums1.len() - 1;
while nums2_index > 0 {
if nums1_index > 0 && nums1[(nums1_index - 1) as usize] > nums2[(nums2_index - 1) as usize]
{
nums1[last_num_index] = nums1[(nums1_index - 1) as usize];
nums1_index -= 1;
} else {
nums1[last_num_index] = nums2[(nums2_index - 1) as usize];
nums2_index -= 1;
}
last_num_index -= 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let mut nums1 = vec![1, 2, 3, 0, 0, 0];
let m = 3;
let mut nums2 = vec![2, 5, 6];
let n = 3;
solution(&mut nums1, m, &mut nums2, n);
assert_eq!(nums1[..6], vec![1, 2, 2, 3, 5, 6]);
}
#[test]
fn example_2() {
let mut nums1 = vec![1];
let m = 1;
let mut nums2 = vec![];
let n = 0;
solution(&mut nums1, m, &mut nums2, n);
assert_eq!(nums1[..1], vec![1]);
}
// #[test]
// fn example_3() {
// let mut nums1 = vec![0];
// let m = 0;
// let mut nums2 = vec![1];
// let n = 1;
// solution(&mut nums1, m, &mut nums2, n);
// assert_eq!(nums1[..1], vec![1]);
// }
}