C++ 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 two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1: nums1 = [1, 3],nums2 = [2], The median is 2.0
最简单的做法,将两个数组合并,然后找中间元素。
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int max = ;
int m = nums1.size() + nums2.size();
int nums[m];
int i = , j = , f = ;
while(i<nums1.size() || j<nums2.size()){
int k = (i<nums1.size()?nums1[i]:max)<=(j<nums2.size()?nums2[j]:max)?(nums1[i++]):(nums2[j++]);
nums[f++] = k;
}
float ans = m%==?((nums[m/]+nums[m/-])/2.0):(nums[(m-)/]);
return ans;
}
};
这个解法新开辟了一个新数组用来存放合并了的两个数组,空间复杂度为O(n+m)。运行时间88ms,击败7%的人。ps:果真菜。
提交过程中犯了一个小错误, float i = 3/2; // i为1.0,而不是1.5 ps:这都能忘,大概上了家里蹲大学吧。。。
刚开始用的不是数组,而是vector<int>,测试的时候显示内存超出限制,一脸懵。自己在dev上跑显示std::bad_alloc。后面才知道vector里面有一个指针指向一片连续的内存空间,当空间不够装下数据时会自动申请另一片更大的空间,然后把原有数据拷贝过去,接着释放原来的那片空间;vector的内存机制
不用新开辟数组的合并解法,不用遍历两个数组,只需要遍历两个数组总长的一半,68ms,击败30%:
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int max = ;
int m = nums1.size() + nums2.size() ;
int i = , j = , f = , k, kk;
int end = m/; //m为偶数,找到m/2和m/2-1,m为奇数,找到m/2
while(f<=end){
kk = k; //用来记录m/2-1
k = (i<nums1.size()?nums1[i]:max)<=(j<nums2.size()?nums2[j]:max)?(nums1[i++]):(nums2[j++]);
f++;
cout<<k;
}
float ans = m%==?( k+kk)/2.0:k;
return ans;
}
};
继续改进:
m为nums1的长度,n为nums2的长度,若m>n,交换两个数组。寻找i、j满足1) i+j=m-i+n-j(或者m-i+n-j+1) 2) max(nums1[i-1], nums2[j-1])<=min(nums[i],nums2[j]), 则中位数为 (max(nums1[i-1], nums2[j-1])+min(nums1[i],nums2[j]))/2(或者min(nums1[i],nums2[j])
寻找过程:
i = (nums1_l + nums1_r)/2 , j = (m+n)/2-i
if nums1[i-1]<=nums2[j] && nums2[j-1]<=nums1[i] 找到答案。
if nums1[i-1] > nums2[j] nums1_r = i; i应该在左半边
if nums2[i-1] < nums2[j] nums1_l = i; i应该在右半边
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
if (n > m) {
swap(n, m);
swap(nums1, nums2);
}
int l, r, mid, pos, id = (n + m + ) / ;
l = , r = n;
while (l <= r)
{
mid = (l + r) >> ;
pos = id - mid;
if (pos >= && mid < n && nums2[pos - ] > nums1[mid]) l = mid + ;
else if (mid >= && pos < m && nums2[pos] < nums1[mid - ]) r = mid - ;
else break;
}
double ans;
if (mid == ) ans = nums2[pos - ];
else if (pos == ) ans = nums1[mid - ];
else ans = max(nums1[mid - ], nums2[pos - ]);
if ((n + m) & ) return ans;
else
{
if (mid == n) ans = (ans + nums2[pos]) / 2.0;
else if (pos == m) ans = (ans + nums1[mid]) / 2.0;
else ans = (ans + min(nums1[mid], nums2[pos])) / 2.0;
}
return ans;
}
};
时间复杂度为O(log(min(m,n)))
C++ Leetcode Median of Two Sorted Arrays的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- [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 two ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode 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 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- HTTPS 如何保证数据传输的安全性
为什么需要 HTTPS? 我们知道 HTTP 是一个纯文本传输协议,对传输过程中的数据包不进行加密,是明文传输,那这样的话对于介于在发送端和接收端之间的任何 一个节点都能知道传输的内容,这些节点可能是 ...
- shell中的${},##, %% , :- ,:+, ? 的使用
假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个/ 及其左边的字符串:dir1/dir2 ...
- 【Selenium2】【HTMLTestRunner】
在拜读虫师大神的Selenium2+Python2.7时,发现生成HTMLTestRunner的测试报告使用的HTMLTestRunner的模块是用的Python2的语法.而我本人比较习惯与Pytho ...
- JavaScript中的方法事件和函数的方法的三种方法
js中的很多事件 而事件相对应的就是方法(函数 )那么今天所说的就是这三种方法 已onclick事件为例 1: 基本方法 <div id="a" onclick= ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ZZNUOJ 2022 摩斯密码
map打表存一下对应的密码 不会map感觉不好弄这题 #include<stdio.h> #include<string.h> #include<math.h> ...
- C#_方法的重载
方法的重载是一种操作性多态,有的时候,可能需要在多个不同的实现中对不同的数据执行相同的逻辑操作,以writeline方法为例,有时可能想他传递一个整数.两者的具体实现肯定是不同的,但在逻辑上,这个方法 ...
- 将本地项目上传到gitlab下
转载自: https://blog.csdn.net/litianxiang_kaola/article/details/74075151 1.安装git https://git-scm.com ...
- spring cloud: zuul: 微网关-简单使用与路由配置
spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...
- boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用
springboot 与 SpringSecurity整合后,为了防御csrf攻击,只有GET|OPTIONS|HEAD|TRACE|CONNECTION可以通过. 其他方法请求时,需要有token ...