坚持每天刷一道题的小可爱还没有疯,依旧很可爱!

题目: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 

最简单的做法,将两个数组合并,然后找中间元素。

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int max = ;
int m = nums1.size() + nums2.size();
int nums[m];
int i = , j = , f = ;
while(i<nums1.size() || j<nums2.size()){
int k = (i<nums1.size()?nums1[i]:max)<=(j<nums2.size()?nums2[j]:max)?(nums1[i++]):(nums2[j++]);
nums[f++] = k;
} float ans = m%==?((nums[m/]+nums[m/-])/2.0):(nums[(m-)/]);
return ans;
}
};

这个解法新开辟了一个新数组用来存放合并了的两个数组,空间复杂度为O(n+m)。运行时间88ms,击败7%的人。ps:果真菜。

提交过程中犯了一个小错误, float i = 3/2;   // i为1.0,而不是1.5   ps:这都能忘,大概上了家里蹲大学吧。。。

刚开始用的不是数组,而是vector<int>,测试的时候显示内存超出限制,一脸懵。自己在dev上跑显示std::bad_alloc。后面才知道vector里面有一个指针指向一片连续的内存空间,当空间不够装下数据时会自动申请另一片更大的空间,然后把原有数据拷贝过去,接着释放原来的那片空间;vector的内存机制

不用新开辟数组的合并解法,不用遍历两个数组,只需要遍历两个数组总长的一半,68ms,击败30%:

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int max = ;
int m = nums1.size() + nums2.size() ;
int i = , j = , f = , k, kk;
int end = m/; //m为偶数,找到m/2和m/2-1,m为奇数,找到m/2
while(f<=end){
kk = k; //用来记录m/2-1
k = (i<nums1.size()?nums1[i]:max)<=(j<nums2.size()?nums2[j]:max)?(nums1[i++]):(nums2[j++]);
f++;
cout<<k;
}
float ans = m%==?( k+kk)/2.0:k;
return ans;
}
};

继续改进:

m为nums1的长度,n为nums2的长度,若m>n,交换两个数组。寻找i、j满足1) i+j=m-i+n-j(或者m-i+n-j+1)  2) max(nums1[i-1], nums2[j-1])<=min(nums[i],nums2[j]), 则中位数为 (max(nums1[i-1], nums2[j-1])+min(nums1[i],nums2[j]))/2(或者min(nums1[i],nums2[j])

寻找过程:

i = (nums1_l + nums1_r)/2 , j = (m+n)/2-i

if  nums1[i-1]<=nums2[j] &&  nums2[j-1]<=nums1[i]  找到答案。

if nums1[i-1] > nums2[j]     nums1_r = i;  i应该在左半边

if nums2[i-1] < nums2[j]    nums1_l = i;    i应该在右半边

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
if (n > m) {
swap(n, m);
swap(nums1, nums2);
}
int l, r, mid, pos, id = (n + m + ) / ;
l = , r = n;
while (l <= r)
{
mid = (l + r) >> ;
pos = id - mid;
if (pos >= && mid < n && nums2[pos - ] > nums1[mid]) l = mid + ;
else if (mid >= && pos < m && nums2[pos] < nums1[mid - ]) r = mid - ;
else break;
}
double ans;
if (mid == ) ans = nums2[pos - ];
else if (pos == ) ans = nums1[mid - ];
else ans = max(nums1[mid - ], nums2[pos - ]);
if ((n + m) & ) return ans;
else
{
if (mid == n) ans = (ans + nums2[pos]) / 2.0;
else if (pos == m) ans = (ans + nums1[mid]) / 2.0;
else ans = (ans + min(nums1[mid], nums2[pos])) / 2.0;
}
return ans;
}
};

时间复杂度为O(log(min(m,n)))

C++ Leetcode Median of Two Sorted Arrays的更多相关文章

  1. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  2. [LeetCode] 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 @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

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

  5. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  6. Leetcode: Median of Two Sorted Arrays. java.

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  7. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  8. leetcode:Median of Two Sorted Arrays分析和实现

    这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...

  9. LeetCode Median of Two Sorted Arrays 找中位数(技巧)

    题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...

随机推荐

  1. 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll

    今天想把自己电脑上的python2换成python3时,安装完python3后,命令行启动时需要出现了上述错误,在网上查了资料后应该是库文件遭到了破坏,于是我下了一个东西安装后就解决了,如果出现了此问 ...

  2. JS实现ul,li排序效果

    <!DOCTYPE html> <html> <head> <title>js列表排序</title> <meta charset=& ...

  3. 【BZOJ】1187: [HNOI2007]神奇游乐园

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1187 每个格子都具有权值,求任意一个回路使得路径上的权值和最大. 裸的插头DP,注意一下几 ...

  4. hdu 5795 A Simple Nim 博弈sg函数

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Pro ...

  5. uoj #228. 基础数据结构练习题 线段树

    #228. 基础数据结构练习题 统计 描述 提交 自定义测试 sylvia 是一个热爱学习的女孩子,今天她想要学习数据结构技巧. 在看了一些博客学了一些姿势后,她想要找一些数据结构题来练练手.于是她的 ...

  6. django分页 Paginator

    分页功能是几乎所有的网站上都需要提供的功能,当你要展示的条目比较多时,必须进行分页,不但能减小数据库读取数据压力,也有利于用户浏览. Django又很贴心的为我们提供了一个Paginator分页工具, ...

  7. 学习笔记17—circos安装集(window环境)

    Windows7环境下Circos使用教程 一.下载安装软件包 1.strawberry perl 因为Circos软件是依赖perl语言编译环境的,但是windows环境下默认是没有perl的,所以 ...

  8. opencv4.0 cuda10 编译速度太慢

    opencv4.0 cuda10 对比不带cuda的时候,编译速度太慢,主要卡在cuda的编译上. 参考http://answers.opencv.org/question/5090/why-open ...

  9. feature map 大小以及反卷积的理解

    (1)边长的计算公式是:  output_h =(originalSize_h+padding*2-kernelSize_h)/stride +1 输入图片大小为200×200,依次经过一层卷积(ke ...

  10. 关于MySQL大量数据分页查询优化

    select * form user id in(select id from user limit 1000000,10);