刷题4. Median of Two Sorted Arrays
2024-08-28 16:40:37
一、题目
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;
}
三、改进
我先思考一下...
最新文章
- Tween Animation----Rotate旋转动画
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
- Java api 入门教程 之 JAVA的StringBuffer类
- Android 3D滑动菜单完全解析,实现推拉门式的立体特效
- AD管理命令
- solr查询在solrconfig.xml中的配置
- 知识库系统/知识管理系统 WCP
- this关键字之一个有趣的用法
- 自己手动绿色化MyEclipse
- 在 Parallels Desktop 中,全屏模式使用 Win7,唤醒时黑屏
- Power Pivot表属性无法切换回表预览模式的问题
- css3 移动端 开关效果
- React 精要面试题讲解(一) 单向数据流
- 一个表里有多个字段需要同时使用字典表进行关联显示,如何写sql查询语句
- Ubuntu下创建XFS文件系统的LVM
- react-native android textinput显示不全的问题
- html class选择器与id选择器
- 【Google设计冲刺】一种适合于创新小组的协作方式
- Linux学习笔记之一————什么是Linux及其应用领域
- CentOS6.3重新加载网卡报错 Active connection path: /org/freedesktop/NetworkManager/ActiveConnection
热门文章
- git pull报错you do not have permission to pull from the repository
- Windows新建域时 administrator账户密码不符合要求解决办法~!
- Java基本语法--程序流程控制
- OpenCV: “vector”: 未声明的标识符和Vector不是模板
- 油候插件grant的使用
- MySQL | 查看log日志
- python2下解决json的unicode编码问题
- 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)
- 分析https网页加载http资源导致的页面报错原因及其解决方案
- 解决使用git出现 The file will have its original line endings in your working directory