一、题目

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. 题解 AT4164 【[ABC102A] Multiple of 2 and N】

    首先我们先来回忆一下小学一年级就学过的知识:任何一个偶数都是 \(2\) 的倍数,那么我们就可以分成两种情况考虑:奇数和偶数. 对于偶数,我们可以直接将其输出,因为它必定能被 \(2\) 与它自己整除 ...

  2. 数位dp(模板+例题)

    文章参考:数位dp之总结 首先,什么是数位dp?它是干什么的? 数位dp是一种计数用的dp,一般就是要统计一个区间[le,ri]内满足一些条件数的个数. 举个栗子: 加入我们要枚举所有上界不超过231 ...

  3. 实现Windows数据更新

    一.枚举 枚举是一组描述性的名称 定义一组有限的值,不能包含方法 对可能的值进行约束 .定义枚举类 public enum Gender { Male,Female } .使用枚举表示整数值 publ ...

  4. brew 安装 yarn 时候失败

    1.mac 安装 brew install yarn 报错 Error: Failure while executing; `git config --local --replace-all home ...

  5. centOS添加ipv6支持(仅限已分配ipv6地址和网关)

    https://blog.csdn.net/cnmilan/article/details/8493977 CentOS 环境下 IPv6设置方法: 1)/etc/sysconfig/network  ...

  6. 【C语言】两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单,a说他不和x比,c说他不和x,z比,编写程序找出三对赛手名单。

    问题分析:假设a是A的对手,b是B的对手,c是C的对手,a,b,c分别是x,y,z之一,且a,b,c互不相等,同时还要满足条件a!= 'x'&&c != 'x'&&c ...

  7. R-CNN系列阅读笔记

    之前感觉研究的还是不够透彻,这次彻底从头到尾研究一下. R-CNN系列 R-CNN 本文发表于2014年. 背景及整体框架 背景:将CNN在图像分类领域的成功(2012年)应用于目标检测上面.检测问题 ...

  8. C++-POJ2234-Matches Game[Nim][SG函数]

    #include <set> #include <map> #include <cmath> #include <queue> #include < ...

  9. C语言各语句的作用

    #include <stdio.h> 在使用标准函数库中的输入输出函数时,编译系统要求程序提供有关的信息(例如对这些输入输出函数的声明),#include<stdio.h>的作 ...

  10. Spring MVC3 + Ehcache 缓存实现

    转自:http://www.coin163.com/it/490594393324999265/spring-ehcache Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了 ...