一、题目

Median of Two Sorted Arrays,具体请自行搜索。

这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂。

但要做到bug free也不难,最大的问题是性能问题。

性能只有42%的样子,内存占用太多。还需要进一步优化!!!

二、这个题目,我自己实现

提交了2次:

第1次: Wrong Answer

第2次:终于对了

下面是我的完整代码实现,需要的拿去:

#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size();
int n = nums2.size();
float f = 0;
vector<int> res;
int i=0,j=0;
while(i<m && j<n){
if(nums1[i]<nums2[j]){
res.push_back(nums1[i]);
i++;
}else{
res.push_back(nums2[j]);
j++;
}
}
while(i<m){
res.push_back(nums1[i]);
i++;
}
while(j<n){
res.push_back(nums2[j]);
j++;
} if((m+n) %2 == 0){
//总共有偶数个,取中间2个平均值
f = res[(m+n)/2-1]+res[(m+n)/2];
return f/2;
}else{
//找到中间值
return res[(m+n)/2];
}
}
}; int main(){
vector<int> v1 = {1,3};
vector<int> v2 = {2};
Solution s;
cout<<s.findMedianSortedArrays(v1,v2)<<endl; v1 = {1,2};
v2 = {3,4};
cout<<s.findMedianSortedArrays(v1,v2)<<endl;
return 0;
}

三、改进

我先思考一下...

刷题4. Median of Two Sorted Arrays的更多相关文章

  1. leetcode第四题:Median of Two Sorted Arrays (java)

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

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

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

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

  4. 算法题之Median of Two Sorted Arrays

    这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1  ...

  5. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  6. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  7. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

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

  8. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  9. [LintCode] 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 ...

随机推荐

  1. WebMethods开发入门

    webMethods  Integration Platform 由用于设计.执行和管理集成解决方案的 3 类组件构成. 设计时组件:这些组件提供了开发和测试集成解决方案的工具. 1.webMetho ...

  2. 深入浅出Mybatis系列七-mapper映射文件配置之insert、update、delete

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(六)---objectFactory.p ...

  3. 深入理解IP之CIDR

    现代IP基于分类的IP越来越少,而基于CIDR的方式的越来越多.那么可以看下面这篇文章: https://www.cnblogs.com/hark0623/p/6547432.html 这篇文章对CI ...

  4. SpringBoot之Configuration

      在SpringBoot中可以通过@Configuration对某个类注解将该类申明为配置类,以此在代替先前spring版本中配置xml中的功能,并且增加了可读性与维护性.并且在注解类中的类方法中可 ...

  5. Go-结构体,结构体指针和方法

    https://cloud.tencent.com/developer/article/1482382 4.1.结构体 结构体:讲一个或多个变量组合到一起形成新的类型,这个类型就是结构体,结构体是值类 ...

  6. 用Java在excel单元格中设置超链接

    (一)问题引入 有时候我们在导入数据到excel中时可能要给某个文件或图片设置超链接,例如链接到外网或者是本地的某个目录.我们可以通过Java代码来实现,借助POI库. (二)解决方案 下面直接给出参 ...

  7. babel plugin

    a = () => {}, // Support for the experimental syntax 'classProperties' isn't currently enabled ya ...

  8. py二级习题

    题目:使用turtle库的turtle.forward(),turtle.left(),函数绘制一个六边形,边长为100 像素 import turtle for i in range(6): tur ...

  9. 《操作系统真象还原》bochs安装

    在安装bochs之前,我们先需要安装虚拟机和linux发行版,也可以安装双系统,总之有个linux操作系统就好. 我是在ubuntu14.04系统下安装bochs的. 安装Bochs 以下为安装步骤 ...

  10. PP: Think globally, act locally: A deep neural network approach to high-dimensional time series forecasting

    Problem: high-dimensional time series forecasting ?? what is "high-dimensional" time serie ...