刷题4. Median of Two Sorted Arrays
一、题目
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的更多相关文章
- 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 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- 【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 ...
- 算法题之Median of Two Sorted Arrays
这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1 ...
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- (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 ...
- 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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [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 ...
随机推荐
- 从零开始的JAVA(1).输入
理一遍JAVA的输入和输出,跟C语言还是有点不用的,虽然以前学过一点.不过好像忘的一干二净了哈哈.从头来一遍把,这次跟着翁恺老师GOGO 1.输出语句:System.out.println();--快 ...
- laravel中{{}}和{!! !!}的区别
1.{{}}和{!! !!} 中{{}}支持转义 一段html代码只是被当成普通的字符串输出 ,{!! !!} 不支持转移 一段html代码可以被正常的解析 1.2具体什么意思呢我们上代码演 ...
- 51Nod 1182 完美字符串 (贪心)
约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同. ...
- C语言 goto
C语言 goto 功能:无条件跳转.不推荐使用 案例 #include <stdio.h> int main() { // 函数跳转.循环跳转 // 创建标志位开始 // 无条件跳转到En ...
- [CF1304F] Animal Observation - dp,单调队列
设 \(f[i][j]\) 为第 \(i\) 天在第 \(j\) 个位置放置的最大值,设 \(s[i][j]\) 是第 \(i\) 行的前缀和,则 \[ \begin{align} f[i][j] & ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- Linux DataGuard --
概述 Oracle Data Guard 是针对企业数据库的最有效和最全面的数据可用性.数据保护和灾难恢复解决方案.它提供管理.监视和自动化软件基础架构来创建和维护一个或多个同步备用数据库,从而保 ...
- badge徽章、挂件模版
markdown语法 Gitee卡片 Gitee挂件 Github挂件 Gitee卡片 [ #输出 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) ...
- Codeforces Round #601 (Div. 2) D Feeding Chicken
//为了连贯,采取一条路形式,从第一行开始 也就是s型 #include <bits/stdc++.h> using namespace std; ; char str[MAXN][MAX ...