diff options
Diffstat (limited to 'median-of-two-sorted-arrays')
-rw-r--r-- | median-of-two-sorted-arrays/Cargo.toml | 8 | ||||
-rw-r--r-- | median-of-two-sorted-arrays/src/main.rs | 23 |
2 files changed, 31 insertions, 0 deletions
diff --git a/median-of-two-sorted-arrays/Cargo.toml b/median-of-two-sorted-arrays/Cargo.toml new file mode 100644 index 0000000..58b9f7b --- /dev/null +++ b/median-of-two-sorted-arrays/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "median-of-two-sorted-arrays" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/median-of-two-sorted-arrays/src/main.rs b/median-of-two-sorted-arrays/src/main.rs new file mode 100644 index 0000000..a8557aa --- /dev/null +++ b/median-of-two-sorted-arrays/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + println!("Hello, world!"); + let tests = vec![ + (vec![1,3], vec![2], 2.0), + (vec![1,2], vec![3,4], 2.5) + ]; + for test in tests { + println!("{:?}{:?} is {} should be {}", test.0, test.1, + Solution::find_median_sorted_arrays(test.0.clone(), test.1.clone()), test.2); + } +} + +struct Solution {} +impl Solution { + pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 { + let mut a = nums1.clone(); + a.append(&mut nums2.clone()); + a.sort(); + let median = a.get((a.len() / 2)-1..((a.len() + 1)/2)).unwrap(); + println!("{:?}", median); + (median.iter().sum::<i32>() as f64) / (median.len() as f64) + } +} |