LeetCode | 4. Median of Two Sorted Arrays(中位数)
题目
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]The median is (2 + 3)/2 = 2.5
我的思路
此题主要运用到合并排序中的思想。
想要求两个有序数组的中位数,可以考虑将这两个数组 nums1
和 nums2
合成一个有序数组 nums3
,这样通过求出 nums3
中间的索引,就可以得到 nums3
数组的中位数(此处注意区分 nums3
的长度为偶数和奇数时的情况即可),即可得到题目所要的结果。
而将这两个有序数组和合成一个有序数组的过程则可以引用合并排序中的思想了(MERGE 的过程)。
代码
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {int i = 0, j = 0, k = 0;int nums3Size = nums1Size + nums2Size;int* nums3 = (int*)malloc(nums3Size * sizeof(int));int medianIdx = 0;double median = 0.0;// Merge nums1 and nums2 to nums3.while((i < nums1Size) && (j < nums2Size)) {if(*(nums1 + i) < *(nums2 + j)) {*(nums3 + k) = *(nums1 + i);i++;k++;}else {*(nums3 + k) = *(nums2 + j);j++;k++;}}while(i < nums1Size) {*(nums3 + k) = *(nums1 + i);i++;k++;}while(j < nums2Size) {*(nums3 + k) = *(nums2 + j);j++;k++;}if(nums3Size % 2 == 0) {medianIdx = nums3Size / 2;median = ((double)*(nums3 + medianIdx - 1) + (double)*(nums3 + medianIdx)) / 2;}else {medianIdx = nums3Size / 2;median = *(nums3 + medianIdx);}free(nums3);nums3 = NULL;return median;
}
LeetCode | 4. Median of Two Sorted Arrays(中位数)相关推荐
- LeetCode 4. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode.004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- LeetCode 4 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode - 4. Median of Two Sorted Arrays
Degree Easy ★ (*´╰╯`๓)♬ Description: There are two sorted arrays nums1 and nums2 of size m and n res ...
- java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...
题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...
- leetcode 之Median of Two Sorted Arrays(五)
找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
最新文章
- 哇 跨行竞争 Python竟能替代PS完成海报设计
- Access SQL中Left Join、Right Join和Inner Join的使用
- Python的from import和import的区别?
- 马云害怕的事还是发生了
- js将字符串 YYMMDDHHmmss 转化为 date类型
- MONGODB 与sql聚合操作对应图
- window 平台上面解决不能动态php_mysqli.dll
- 管理感悟:计划是给谁看的
- 飞天技术汇大视频专场:全民视频时代下的创新技术之路
- 多位大厂专家鼎力推荐,44个微服务架构设计模式pdf,程序员福利
- 回弹强度记录表填写_回弹法检测砼抗压强度原始记录表(2011年规程)
- 人工智能-深度学习-手写数字识别
- QT之隐藏任务栏图标
- keras中 shape参数如何设置
- 移动开发框架--------基于腾讯手Q样式规范Frozen UI
- HBase Muti-Master
- 判断位数为3的高级写法
- ASAM让你减肥成为一种乐趣
- 这篇文章终于把中美德三国的工业互联网讲清楚了
- 狂神。Mybatis学习。
热门文章
- 史上最全JavaScript数组方法
- 你可以不精通Vue,但一定要精通JS!
- shell脚本(二)--if流程控制语句和for循环语句的使用
- [zz]使用 SQLAlchemy
- 休闲娱乐 - 经典儿歌收集
- Grasshopper for Rhino 7.0 x64 汉化补丁来了,已经接近100%了
- 鸿蒙渊boss,鸿蒙异动 云光复生!《天下3》全新云光殿副本奥秘重重
- TCP和UDP的笑话导致的悲剧爱情故事
- ubuntu android 加入so文件,Ubuntu下Android NDK配置及生成so文件步骤
- 天猫维护品牌自身形象,大量续签考核不达标账户将被撤出