LeetCode-4. 两个排序数组的中位数(详解)

链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/

有两个大小为 m 和 n 的排序数组 nums1 和 nums2 。

请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) 。

示例 1:

nums1 = [1, 3]
nums2 = [2]
中位数是 2.0

示例 2:

nums1 = [1, 2]
nums2 = [3, 4]
中位数是 (2 + 3)/2 = 2.5

分析

这道题很简单,题目已经说了两个数组的顺序已经是排序好了,所以我们直接用一个数组,然后将数组 nums1 和 nums2 的前面(nums1Size+nums2Size)/2个数据存进去,然后再将中位数return出来即可

代码如下(使用C方式)

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size)
{
  int s[nums1Size+nums2Size];
  int i,L=0,R=0;
  int j=nums1Size+nums2Size;
  for(i=0;i<=(j/2);)  
  {
    if(L==nums1Size)    //排序数组 nums1已经放置完了
    {
      while(i<=(j/2)) //直接放 nums2数组
      s[i++]=nums2[R++];
    }
    else if(R==nums2Size) //排序数组 nums2已经放置完了
    {
      while(i<=(j/2)) //直接放 nums1数组
      s[i++]=nums1[L++];
    }
    else
    {
      if(nums1[L]<nums2[R])
        s[i++]=nums1[L++];
      else
        s[i++]=nums2[R++];
    }
 }

//for(L=0;L<i;L++)
// printf("%d ",s[L]);

//printf("\n%d\n",i);

  if(j%2==1)    //奇数个,则只要最中间值
  {
    return s[i-1] ;
  }
  else
  {
    return (s[i-2]+s[i-1 ])/2.0;
  }
}

原文地址:https://www.cnblogs.com/lifexy/p/8677969.html

时间: 2024-08-30 05:06:13

LeetCode-4. 两个排序数组的中位数(详解)的相关文章

[LeetCode] 4. 两个排序数组的中位数

该题的难度分级是Hard,那么难在哪里呢?我们先来看题目. 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.5 当你看到要求的时间复杂度为O(log (m+n)),你想到了什么?没错,二分法. 从示

Leetcode(4)-两个排序数组的中位数

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.5 自己的思路:既然两个数组都是有序的,我可以将两个数组合并成一个数组,依然有序,然后根据奇偶来判断出中位数. double findMedianSo

LeetCode刷题-004两个排序数组的中位数

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1:nums1 = [1, 3]nums2 = [2]中位数是 2.0 示例 2:nums1 = [1, 2]nums2 = [3, 4]中位数是 (2 + 3)/2 = 2.5 1 class Solution { 2 public: 3 double findMedianSortedArrays(vector<int>&

LeetCode寻找两个有序数组的中位数

题目: 给定两个大小为 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 来源:力扣(LeetCode) 链接:https://leetcode-cn.

两个排序数组的中位数

中英题面 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . There are two sorted arrays nums1 and nums2 of size m and n respectively. 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+

leetcode链表--13、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 sorted arrays. The overall run time complexity should be O(log (m+n)). 题目解析:本题关键之处在于时间复杂度要求为O(log(m+n)),本题如果采用合并并排序两数组的方式,时间复杂度为O((m+n)*log(n+m))但是在牛客网上

两个排序数组求中位数

题目 There are two sorted arrays A and B 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)). 思路 丢掉一个最小的,丢掉一个最大的 如果剩下小于等于两个数字,可以从中得到中位数 标准写法不是这样的,是通过寻找第K小的数来实现的 该方法的核心是将原问题转变成一个

[LintCode] 两个排序数组的中位数

1 class Solution { 2 public: 3 /** 4 * @param A: An integer array. 5 * @param B: An integer array. 6 * @return: a double whose format is *.5 or *.0 7 */ 8 double findMedianSortedArrays(vector<int> A, vector<int> B) { 9 // write your code here

[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)). 解法一:不考虑时间复杂度限制在O(log(m+n)),则将两个数组遍历一遍即可以组合成一个排好序的数组,然后取数组的中位数即可,时间复杂度O(m+n): c