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

题目: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. mysql行转列(多行转一列)

    场景 比如说一个订单对应多条数据,当状态(status)=1的时候,  数量(num)=25,当状态(status)=2的时候,  数量(num)=45,现在想用一条sql记录下不同状态对应的数量为多 ...

  2. Python 爬取 猫眼 top100 电影例子

    一个Python 爬取猫眼top100的小栗子 import json import requests import re from multiprocessing import Pool #//进程 ...

  3. 烽火HG220G-U E00L2.03M2000光猫改桥接教程

    烽火HG220G-U E00L2.03M2000光猫改桥接教程 P.S. 此教程同样适用于HG221G/HG260G-U/HG261G.(2016.12) 随着北京联通从原有的ONU升级到HGU之后, ...

  4. Codeforces 781C Underground Lab

    题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...

  5. mongdb使用技巧

    进入shell的方法:mongo 命令   # 使用系统服务启动 mongodb /etc/init.d/mongod # 或 service mongod start # 或 service mon ...

  6. HDUOJ 不容易系列之(4)——考新郎

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2049 一开始我的想法就是使用错排公式,先使用全排列从N对中选出M对,然后再使用错排对选出的M对进行错排计 ...

  7. anaconda 安装包

    很多包在anaconda navigator里搜不到 如果为了在host编译opencv而在bashrc和profile里屏蔽了anaconda的路径,则 使用$HOME/anaconda3/bin/ ...

  8. Mac python Tesseract 验证码识别

    Tesseract 简介 Tesseract(/'tesərækt/) 这个词的意思是"超立方体",指的是几何学里的四维标准方体,又称"正八胞体".不过这里要讲 ...

  9. Python Appium 滑动、点击等操作

    Python Appium 滑动.点击等操作 1.手机滑动-swipe # FileName : Tmall_App.py # Author : Adil # DateTime : 2018/3/25 ...

  10. feign三:覆写feign的默认配置及feign的日志

    feign三:覆写feign的默认配置及feign的日志 默认配置复写 本项目地址:http://192.168.1.103:7601 本例是通过feign调用 eureka项目中的/eureka/a ...