题目

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

我的思路

此题主要运用到合并排序中的思想。

想要求两个有序数组的中位数,可以考虑将这两个数组 nums1nums2 合成一个有序数组 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(中位数)相关推荐

  1. 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 ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. leetcode - 4. Median of Two Sorted Arrays

    Degree Easy ★ (*´╰╯`๓)♬ Description: There are two sorted arrays nums1 and nums2 of size m and n res ...

  6. java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...

    题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...

  7. leetcode 之Median of Two Sorted Arrays(五)

    找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...

  8. 【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 ...

  9. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...

最新文章

  1. 哇 跨行竞争 Python竟能替代PS完成海报设计
  2. Access SQL中Left Join、Right Join和Inner Join的使用
  3. Python的from import和import的区别?
  4. 马云害怕的事还是发生了
  5. js将字符串 YYMMDDHHmmss 转化为 date类型
  6. MONGODB 与sql聚合操作对应图
  7. window 平台上面解决不能动态php_mysqli.dll
  8. 管理感悟:计划是给谁看的
  9. 飞天技术汇大视频专场:全民视频时代下的创新技术之路
  10. 多位大厂专家鼎力推荐,44个微服务架构设计模式pdf,程序员福利
  11. 回弹强度记录表填写_回弹法检测砼抗压强度原始记录表(2011年规程)
  12. 人工智能-深度学习-手写数字识别
  13. QT之隐藏任务栏图标
  14. keras中 shape参数如何设置
  15. 移动开发框架--------基于腾讯手Q样式规范Frozen UI
  16. HBase Muti-Master
  17. 判断位数为3的高级写法
  18. ASAM让你减肥成为一种乐趣
  19. 这篇文章终于把中美德三国的工业互联网讲清楚了
  20. 狂神。Mybatis学习。

热门文章

  1. 史上最全JavaScript数组方法
  2. 你可以不精通Vue,但一定要精通JS!
  3. shell脚本(二)--if流程控制语句和for循环语句的使用
  4. [zz]使用 SQLAlchemy
  5. 休闲娱乐 - 经典儿歌收集
  6. Grasshopper for Rhino 7.0 x64 汉化补丁来了,已经接近100%了
  7. 鸿蒙渊boss,鸿蒙异动 云光复生!《天下3》全新云光殿副本奥秘重重
  8. TCP和UDP的笑话导致的悲剧爱情故事
  9. ubuntu android 加入so文件,Ubuntu下Android NDK配置及生成so文件步骤
  10. 天猫维护品牌自身形象,大量续签考核不达标账户将被撤出