4. 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
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
==============
找出两个数组的中位数,
中位数的定义是明确的:按序排好的数组,array.size=n是奇数的话,第(n+1)/2个数字;否则就是第n/2和第(n+1)/2的平均数。
==========思路:
划分思路,
第一步:按照归并思路找到两个数组中的第k大
第二步:按照计算中位数的方法,返回中位数。
=========
code实现,
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int lengtha = nums1.size();
int lengthb = nums2.size();
int total = lengtha+lengthb;
if(total & 0x1){
return find_kth(nums1,nums2,total/+);
}else{
return (find_kth(nums1,nums2,total/)+
find_kth(nums1,nums2,total/+))/2.0;
}
}
private:
int find_kth(vector<int> &A,vector<int> &B,int k){
std::vector<int>::const_iterator p1 = A.begin();
std::vector<int>::const_iterator p2 = B.begin();
int m = ;
while(p1!=A.end() && p2!=B.end()){
if(*p1<=*p2 && m==(k-)){
return *p1;
}else if(*p1>*p2 && m==(k-)){
return *p2;
}
if(*p1<=*p2)
p1++;
else
p2++;
m++;
}//while
while(p1!=A.end()){
if(m==(k-)){
return *p1;
}
p1++;
m++;
}
while(p2!=B.end()){
if(m==(k-)){
return *p2;
}
p2++;
m++;
}
return -;
}
};
4. Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- 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 ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- LeetCode--No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
随机推荐
- Cortana 在安装语言包后失灵 | 解决
http://windows.microsoft.com/zh-cn/windows-10/cortanas-regions-and-languages 适用于 Windows 10 Currentl ...
- Java:多个数求和
设计思想: 先定义int n,定义输入数的个数,输入一个新建的动态数组,输入数字存入动态数组中,函数转换并求和,最后输出. 程序流程图: 源程序代码: package com; import java ...
- java如何获取当前机器ip和容器port
获取当前机器ip: private static String getIpAddress() throws UnknownHostException { InetAddress address = I ...
- 理解ros服务和参数 ---- 7
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 周学伟 Description: 本教程介绍了ROS 服务和参数的知识,以及命令行工具rosservi ...
- 某种数列问题 (jx.cpp/c/pas) 1000MS 256MB
众所周知,chenzeyu97有无数的妹子(阿掉!>_<),而且他还有很多恶趣味的问题,继上次纠结于一排妹子的排法以后,今天他有非(chi)常(bao)认(cheng)真(zhe)去研究一 ...
- JS中的自定义属性
<div id="div1" a="a" data-bbb="bbb">div</div> <script&g ...
- iOS 导出 ipa 包时 三个选项的意义
OS 导出 ipa 包时 三个选项的意思 如图 在 iOS 到处 ipa包的时候 会有三个选项 Save for iOS App Store Deployment Save for Ad Hoc D ...
- 如何在远程计算机上运行PowerShell
问题: 不能在WORKGROUP里面的远程计算机里运行PowerShell指令,报错为用户名密码错 解决方法: 把两台机器上(远程计算机其和本机)都加入到trustedhosts 具体请参考 http ...
- eclipse adt 项目依赖,使用git上的项目
从git下载的android工程不能直接导入eclipse,有的项目要依赖这个项目,怎么办呢 好像只能新建一个android项目,然后把libs,res,src,AndroidManifest.xml ...
- 【转】修改 shellstyle.dll 权限
原文网址:http://bbs.zol.com.cn/nbbbs/d160_147794.html 对于经常使用电脑的人来说,比如像最近的我,在晚上总觉得电脑资源管理器屏幕显示得太亮,即使我把电脑的亮 ...