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

题意

有两个排序好的数组nums1 & nums2.找到这两个数组的中位数。

思路

令sum等于两个数组大小的和。

如果sum==1,返回这个数。

否则,如果sum是偶数,返回这两个数组中 第sum/2大 的数 和 第sum/2+1大 的数的均值;

如果sum是奇数,返回这两个数组中第sum/2+1大 的数。

 1 double find(int* nums1, int nums1Size, int* nums2, int nums2Size,int t){ //返回两个数组中第t小的数
 2     int pos1=0,pos2=0;
 3     int ans;
 4     while(t--)
 5     {
 6         if(pos1<nums1Size && pos2<nums2Size)
 7         {
 8             if(nums1[pos1]>nums2[pos2])
 9                ans=nums2[pos2++];
10             else
11                ans=nums1[pos1++];
12         }
13         else if(pos1<nums1Size)
14             ans=nums1[pos1++];
15         else if(pos2<nums2Size)
16             ans=nums2[pos2++];
17     }
18     return ans;
19 }
20 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
21     int sum=nums1Size+nums2Size;
22     if(sum == 1)
23     {
24         if(nums1Size == 1)
25             return nums1[0];
26         else return nums2[0];
27     }
28     else
29     {
30         if(sum%2==0)
31             return (find(nums1,nums1Size,nums2,nums2Size,sum/2)+find(nums1,nums1Size,nums2,nums2Size,sum/2+1))/2;
32          else
33             return find(nums1,nums1Size,nums2,nums2Size,sum/2+1);
34     }
35 }

转载于:https://www.cnblogs.com/madking/p/4657690.html

LeetCode.004 Median of Two Sorted Arrays相关推荐

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

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

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

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

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

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

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

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

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

  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. ML 01、机器学习概论
  2. python进行数据分析,学习笔记 第8章(1)
  3. STM32开发 -- CRC校验码
  4. 什么是对象,为什么要面向对象,怎么才能面向对象?
  5. android jni调用so库
  6. Hyper-V Server联机调整虚拟硬盘大小
  7. 又拍网架构-又一个用到python的网站(转载)
  8. filemode对git diff的影响
  9. openresty安装配置 Ubuntu下
  10. P3512 [POI2010]PIL-Pilots
  11. java查找算法:二分查找(两种方式)
  12. 自在服务器虚拟化,新破晓诛仙3单机版16职业商业仿官青罗天界灵境神隐虚拟机一键端GM网单...
  13. 什么是王道?什么是王道中的王道?
  14. uniapp运行到微信小程序开发工具
  15. 数据分析师岗位 分析可视化
  16. html id命名规范,CSS常用类/ID命名规范
  17. GPS定位(四)-经纬度格式转换-(互转 度转度分秒 度分秒转度……)
  18. Encoded password does not look like BCrypt 使用SpringSecurity中BCryptPasswordEncoder做盐值加密时出现错误
  19. 扩展欧几里德算法解决问题A:Jugs
  20. 图解:为什么总有人能“躺着”挣钱?

热门文章

  1. 【转载】ubuntu换源
  2. android - 小技巧合集(不断更新)
  3. 一道简单的多维数组取值问题
  4. 【学术之门之P2P算法研读】P2P中的Chord算法
  5. 为什么响应式编程并非一时之势?
  6. 如何在windows2003(IIS6)下配置IIS,使其支持cshtml
  7. 多线程之旅七——GUI线程模型,消息的投递(post)与处理
  8. Lighttpd1.4.20源码分析之etag.c(h) -------HTTP/1.1中的Etag域
  9. 大型企业网络配置系列课程详解(六) --PPP链路的配置与相关概念的理解
  10. 使用fieldset、label标签制作form表单