刷题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 ...
随机推荐
- Sql Server2008忘记sa登陆密码
Sql的sa登陆密码忘记解决方法: 语句执行的前提: 1.系统可以登陆进去(当不记得sa密码的时候,可以使用windows用户验证的方式进行登陆) 2.平时用sa登陆,点了记住密码但是不记得密码是多少 ...
- AcWing 6. 多重背包问题 III
//f[i,j] 所有只从前i块能量石中选,且总体积恰好为j的方案数 #include <iostream> #include <algorithm> #include < ...
- Spring Boot的Web配置
一.使用YML文件配置多套环境
- VMware vSphere Client
复制虚拟机 在虚拟机关机状态下,选中一个虚拟机,文件 - 导出 - 导出OVF模板,导出成功后,再文件 - 部署OVF模板(修改IP.MAC.主机名称)
- form表单jquery提交400错误
今天在用jquery提交form表单的时候出现了一个奇怪的400错误,最后发现原因是因为使用了元素选择器来选择form表单,$('form'),使用这种方式导致了某些参数提交不上去,参数匹配不上于是s ...
- 18家大厂Java面试题整理了350道(分布式+微服务+高并发)
一.性能调优系列 1.Tomcat性能调优 JVM参数调优: -Xms 表示JVM初始化堆的大小, -Xmx表示JVM堆的最大值.这两个值的大小一般根据需要进行设置. 当应用程序需要的内存超出堆的最大 ...
- 题解 AT3853 【Otoshidama】
题目传送门. 暴力枚举题. 分析 Step 1:定义两个变量,\(n\)和\(y\). int n,y; cin>>n>>y; Step 2:使用二重循环进行暴力枚举. for ...
- Node.js、npm和webpack的安装
1. 前往Node.js官网下载安装程序 2. 一路点击下一步即可 3. 测试是否安装成功 4. 配置npm在安装全局模块时的路径和缓存cache的路径 因为在执行例如npm install webp ...
- JS高级---数组和伪数组
数组和伪数组 伪数组和数组的区别 真数组的长度是可变的 伪数组的长度不可变 function f1() { var sum = 0; for (var i = 0; i < arguments ...
- pl/sql developer试用期结束
(产品代码)Product Code:ljkfuhjpccxt8xq2re37n97595ldmv9kch (序列号)Serial Number:302967 (口令)Password:xs374ca ...