Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1917 from AkifhanIlgaz/0088
Browse files Browse the repository at this point in the history
Create: 0088-merge-sorted-array.rs
  • Loading branch information
tahsintunan authored Jan 7, 2023
2 parents 45de2f5 + 1b78fdc commit 9896d9c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rust/0088-merge-sorted-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
impl Solution {
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
let (mut m, mut n) = (m as usize, n as usize);
// Last index nums1
let mut last = m + n - 1;

// Merge in reverse order
while m > 0 && n > 0 {
if nums1[m - 1] > nums2[n as usize - 1] {
nums1[last] = nums1[m - 1];
m -= 1;
} else {
nums1[last] = nums2[n - 1];
n -= 1;
}
last -= 1
}

// Fill nums1 with leftover nums2 elements
while n > 0 {
nums1[last] = nums2[n - 1];
n -= 1;
last -= 1;
}
}
}

0 comments on commit 9896d9c

Please sign in to comment.