-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bee8a22
commit 9e4ab5a
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "q88_merge_sorted_array" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[dev-dependencies] | ||
rstest = "0.23.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
// https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg | ||
// pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) | ||
|
||
pub fn merge(nums1: &mut [i32], m: i32, nums2: &[i32], n: i32) { | ||
assert_eq!(nums2.len(), n as usize); | ||
if n == 0 { | ||
//println!("{:?}", nums1); | ||
return; | ||
} | ||
|
||
if m == 0 { | ||
nums1 | ||
.iter_mut() | ||
.zip(nums2.iter()) | ||
.for_each(|(v1, v2)| *v1 = *v2); | ||
|
||
//println!("{:?}", nums1); | ||
return; | ||
} | ||
|
||
let mut tail1 = m as usize - 1; | ||
let mut i = (m + n) as usize - 1; | ||
//dbg!((m, n)); | ||
|
||
/* nums1.iter_mut().rev().for_each(|v| { | ||
*v = 0; | ||
}); */ | ||
|
||
let mut is_nums1_done = false; | ||
|
||
for tail2 in (0..n as usize).rev() { | ||
//dbg!((i, tail1, tail2)); | ||
//dbg!((&nums1, &nums2)); | ||
while nums1[tail1] > nums2[tail2] { | ||
nums1[i] = nums1[tail1]; | ||
i -= 1; | ||
|
||
if let Some(v) = tail1.checked_sub(1) { | ||
tail1 = v; | ||
} else { | ||
is_nums1_done = true; | ||
break; | ||
} | ||
|
||
//dbg!((i, tail1, tail2)); | ||
//dbg!((&nums1, &nums2)); | ||
} | ||
|
||
nums1[i] = nums2[tail2]; | ||
|
||
if is_nums1_done { | ||
nums1 | ||
.iter_mut() | ||
.zip(nums2.iter()) | ||
.take(tail2) | ||
.for_each(|(v1, v2)| *v1 = *v2); | ||
|
||
break; | ||
} | ||
|
||
i -= 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
// https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg | ||
// pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) | ||
|
||
pub fn merge(nums1: &mut [i32], m: i32, nums2: &[i32], n: i32) { | ||
let mut merge_index = (m + n - 1) as usize; | ||
|
||
let mut is_nums1_done = true; | ||
let mut index1 = 0; | ||
if m > 0 { | ||
index1 = (m - 1) as usize; | ||
is_nums1_done = false; | ||
} | ||
|
||
for v in nums2.iter().rev() { | ||
if !is_nums1_done { | ||
while nums1[index1] > *v { | ||
nums1[merge_index] = nums1[index1]; | ||
merge_index -= 1; | ||
|
||
match index1.checked_sub(1) { | ||
Some(result) => index1 = result, | ||
None => { | ||
is_nums1_done = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
nums1[merge_index] = *v; | ||
match merge_index.checked_sub(1) { | ||
Some(result) => merge_index = result, | ||
None => break, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pub struct Solution; | ||
|
||
// Generated by ChatGPT o1-preview, which is both clean and concise, AWESOME! | ||
|
||
impl Solution { | ||
// https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg | ||
// pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) | ||
|
||
pub fn merge(nums1: &mut [i32], m: usize, nums2: &[i32], n: usize) { | ||
let mut i = m; | ||
let mut j = n; | ||
let mut k = m + n; | ||
|
||
while j > 0 { | ||
if i > 0 && nums1[i - 1] > nums2[j - 1] { | ||
nums1[k - 1] = nums1[i - 1]; | ||
i -= 1; | ||
} else { | ||
nums1[k - 1] = nums2[j - 1]; | ||
j -= 1; | ||
} | ||
k -= 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod impl_v1; | ||
pub mod impl_v2; | ||
pub mod impl_v3; |
30 changes: 30 additions & 0 deletions
30
implementations/q88_merge_sorted_array/tests/test_all_impl.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rstest::rstest; | ||
|
||
use q88_merge_sorted_array::impl_v1::Solution as Solution_v1; | ||
use q88_merge_sorted_array::impl_v2::Solution as Solution_v2; | ||
use q88_merge_sorted_array::impl_v3::Solution as Solution_v3; | ||
|
||
#[rstest] | ||
#[case(vec![1, 2, 3, 0, 0, 0], vec![2, 5, 6], vec![1, 2, 2, 3, 5, 6])] | ||
#[case(vec![1], vec![], vec![1])] | ||
#[case(vec![0], vec![1], vec![1])] | ||
#[case(vec![1, 2, 3], vec![], vec![1, 2, 3])] | ||
#[case(vec![0, 0, 0], vec![2, 5, 6], vec![2, 5, 6])] | ||
#[case(vec![2, 0], vec![1], vec![1, 2])] | ||
#[case(vec![4, 5, 6, 0, 0, 0], vec![1, 2, 3], vec![1, 2, 3, 4, 5, 6])] | ||
fn test_all_impl(#[case] nums1: Vec<i32>, #[case] nums2: Vec<i32>, #[case] expected: Vec<i32>) { | ||
let n = nums2.len() as i32; | ||
let m = nums1.len() as i32 - n; | ||
|
||
let mut result_v1 = nums1.clone(); | ||
Solution_v1::merge(&mut result_v1, m, &nums2, n); | ||
assert_eq!(expected, result_v1); | ||
|
||
let mut result_v2 = nums1.clone(); | ||
Solution_v2::merge(&mut result_v2, m, &nums2, n); | ||
assert_eq!(expected, result_v2); | ||
|
||
let mut result_v3 = nums1.clone(); | ||
Solution_v3::merge(&mut result_v3, m as usize, &nums2, n as usize); | ||
assert_eq!(expected, result_v3); | ||
} |