LeetCode004-寻找两个正序数组的中位数(Median of two sorted arrays)-Rust语言实现
题目
给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。
请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
- 示例 1:
nums1 = [1, 3]
nums2 = [2]
则中位数是 2.0
- 示例 2:
nums1 = [1, 2]
nums2 = [3, 4]
则中位数是 (2 + 3)/2 = 2.5
思路:
利用二分查找和栈解决思路:
假设两个数组分别为:
m = [23, 69, 180, 400, 500, 600, 800]
n = [2 , 14, 36, 57, 99, 110]
取m[0],n[0]较大的一个,即m[0] = 23
利用二分法在n中找出所有小于23的数,入栈 stack = [2, 14], 此时m, n分别为:
m = [23, 69, 180, 400, 500, 600, 800] n = [36, 57, 99, 110]
重复执行步骤1,2直到len(stack)长度不小于avg_len = (len(m)+len(n))/2
结果即为stack[avg_len] 或者 (stack[stack-1] + stack[stack])/2
提示:此道题目的边界条件很多,需要格外注意
struct Solution {}pub fn get_binary_insert_index(nums: &Vec<i32>, l: usize, r: usize,num: i32) -> usize {// 2 14 36 57 99 110// ^// 23// index is 2let mut left_index = l;let mut right_index = r;if num <= nums[left_index] {left_index = if num == nums[left_index] { left_index + 1 } else { left_index };return left_index;}if num >= nums[right_index] {return right_index + 1;}let mut half_index = (left_index + right_index) / 2;loop {if nums[half_index] >= num {right_index = half_index;} else {left_index = half_index;}if ((right_index - left_index) as i32) <= 1 {break;}half_index = (left_index + right_index) / 2;}right_index
}impl Solution {pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {let v1 = &nums1;let v2 = &nums2;let total_len = v1.len() + v2.len();let avg_len = total_len / 2;let mut index1 = 0;let mut index2 = 0;let mut stack = Vec::new();loop {if index1 >= v1.len() && index2 >= v2.len() {break}if index1 >= v1.len() {stack.push(v2[index2]);index2 = index2 + 1;continue}if index2 >= v2.len() {stack.push(v1[index1]);index1 = index1 + 1;continue}if v1[index1] > v2[index2] {let index_tmp = index2;index2 = get_binary_insert_index(v2, index2, v2.len() - 1, v1[index1]);for i in index_tmp..index2 {stack.push(v2[i]);}} else {let index_tmp = index1;index1 = get_binary_insert_index(v1, index1, v1.len() - 1,v2[index2]);for i in index_tmp..index1 {stack.push(v1[i]);}}if index1 + index2 > avg_len {break;}}if total_len % 2 == 1 {return stack[avg_len] as f64;}((stack[avg_len - 1] + stack[avg_len]) as f64) / 2.0}
}fn case1() {let nums1 = vec![1, 3];let nums2 = vec![2];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(2.0, ret);
}fn case2() {let nums1 = vec![1, 2];let nums2 = vec![3, 4];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(2.5, ret);
}fn case3() {let nums1 = vec![2, 14, 36, 57, 99, 110];let nums2 = vec![23, 69, 180, 400, 500, 600, 800];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(99.0, ret);
}fn case4() {let nums1 = vec![0, 0];let nums2 = vec![0, 0];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(0.0, ret);
}fn case5() {let nums1 = vec![];let nums2 = vec![1];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(1.0, ret);
}fn case6() {let nums1 = vec![1,1,3,3];let nums2 = vec![1,1,3,3];let ret = Solution::find_median_sorted_arrays(nums1, nums2);println!("[find_median_sorted_arrays] Solution result: {}", ret);assert_eq!(2.0, ret);
}
fn main() {case1();case2();case3();case4();case5();case6();
}
LeetCode004-寻找两个正序数组的中位数(Median of two sorted arrays)-Rust语言实现相关推荐
- LeetCode 4 寻找两个正序数组的中位数
https://leetcode-cn.com/problems/median-of-two-sorted-arra 解决方案 Go 版本 func findMedianSortedArrays(nu ...
- 【LeetCode】4.寻找两个正序数组的中位数
4.寻找两个正序数组的中位数 一.问题描述 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 二.问题简化 所谓中位数,就 ...
- 【LeetCode】【HOT】4. 寻找两个正序数组的中位数(二分查找)
[LeetCode][HOT]4. 寻找两个正序数组的中位数 文章目录 [LeetCode][HOT]4. 寻找两个正序数组的中位数 package hot;public class Solution ...
- [二分搜索|快速选择] leetcode 4 寻找两个正序数组的中位数
[二分搜索|快速选择] leetcode 4 寻找两个正序数组的中位数 1.题目 题目链接 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组 ...
- 2022-6-13 全O(1)的数据结构,两数相加,无重复字符的最长子串,寻找两个正序数组的中位数,盛最多水的容器,......
1. 全 O(1) 的数据结构 Design a data structure to store the strings' count with the ability to return the s ...
- 寻找两个正序数组的中位数
寻找两个正序数组的中位数 时隔许久,我又回来了. 题目:给定两个大小分别为m和n的正序(从小到大)数组nums1和nums2.请你找出并返回这两个正序数组的中位数. 要求算法的时间复杂度为O(log( ...
- 算法:寻找两个正序数组的中位数。
给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 ...
- 寻找两个正序数组的中位数——冒泡排序(归并排序)
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 冒泡算法 class Solution {public double f ...
- Leetcode每日必刷题库第4题,如何寻找两个正序数组的中位数?
题目: 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
最新文章
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
- 短序列组装Sequence Assembly(转载)
- jQuery数据转换与提交
- 经典面试题(34):以下代码将输出的结果是什么?
- 【Elasticsearch】Elasticsearch 6.0中节省空间的改进
- 杭电1862EXCEL排序
- Android音视频从入门到提高---任务列表
- 如何免费将XPS转换为PDF格式
- 数理统计基础 统计量
- Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
- 计算机开机弹出的今日热点怎么关闭
- 信呼协同办公系统-存储型XSS漏洞
- 流量高峰时期的性能瓶颈有哪些、以及如何来解决
- excel行数据不规则解析匹配
- 2022抖音温暖中国年 集年味分7亿玩法攻略
- gcc / -Wl,-Bsymbolic
- ICP波长及分析校正
- 【一文弄懂】张正友标定法-完整学习笔记-从原理到实战
- 虚拟机可以当成服务器吗,虚拟机不仅仅可以用于部署服务器功能
- 中国最强AI超级服务器问世,每秒提供AI计算2000万亿次
热门文章
- binlog日志_mysql的binlog日志的自动定时清理
- day18-基于DjangoORM的外键实现增加用户(含select标签)
- BZOJ1901:Zju2112 Dynamic Rankings——题解
- 网络访问保护(NAP)技术之详解
- CRM系统主要业务流程思维导图
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
- 6、Actor,Stage的学习
- 适用于各种连锁企业15寸多点触摸android收款机消费机pos机
- 在适当的场合使用FlagsAttribute修饰枚举
- 终于去掉了location.reload()弹出的问题。