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 ...
随机推荐
- Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- Mysql 下 Insert、Update、Delete、Order By、Group By注入
Insert: 语法:INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) 报错注入: insert into test(id,name,p ...
- CString用法总结
概述:CString是MFC中提供的用于处理字符串的类,是一种很有用的数据类型. 它很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作时方便了很多. 不管怎样,使用CString有很多的特殊 ...
- Vue.js相关知识2-组件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- URAL(timus) 1280 Topological Sorting(模拟)
Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...
- 【NOIP2012】国王游戏
这一次高精度完美地过辣好开心OvO,还get到了非常方便的高精度除小于10000的方法,这个是我自己脑出来的OvO 看来下午高精度傻逼得值qvq 原题: 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个 ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- DDD, MVC & Entity Framework
https://digitalpolis.co.uk/software-thoughts/ddd-mvc-entity-framework/ Data Points - Coding for Doma ...
- Maximum number of WAL files in the pg_xlog directory (1)
Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue ...
- 无法在web服务器上启动调试。Microsoft Visual Studio 远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行,VS2012调试错误
1.重启(无用) 2.关闭防火墙(无用) 3.开启文件与打印机共享(无用) 4.无远程调试权限,改为本地调试.或者是IIS中此项目没有启动.或者没有在IIS中新建此项目.