Median of Two Sorted Arrays-----LeetCode
There are two sorted arrays A and B 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)).
解题思路:
该题可以解决所有求有序数组A和B有序合并之后第k小的数!
该题的重要结论:
如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。
具体的分析过程可以参考http://blog.csdn.net/zxzxy1988/article/details/8587244
class Solution {
public:
double findKth(int A[], int m, int B[], int n, int k)
{
//m is equal or smaller than n
if (m > n)
return findKth(B, n, A, m, k);
if (m == )
return B[k-];
if (k <= )
return min(A[], B[]);
int pa = min(k / , m), pb = k - pa;
if (A[pa-] < B[pb-])
{
return findKth(A + pa, m - pa, B, n, k - pa);
}
else if(A[pa-] > B[pb-])
{
return findKth(A, m, B + pb, n - pb, k - pb);
} else
return A[pa-];
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int k = m + n;
if (k & 0x1)
{
return findKth(A, m, B, n, k / + );
} else
{
return (findKth(A, m, B, n, k / ) + findKth(A, m, B, n, k / + )) / ;
}
}
};
Median of Two Sorted Arrays-----LeetCode的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 3.Median of Two Sorted Arrays Leetcode
难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 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 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- 每个极客都应该知道的Linux技巧
每个极客都应该知道的Linux技巧 2014/03/07 | 分类: IT技术 | 0 条评论 | 标签: LINUX 分享到:18 本文由 伯乐在线 - 欣仔 翻译自 TuxRadar Linux. ...
- C语言之宏
所谓的宏就是一种预处理命令,什么是与处理呢?即在编译过程之前先对程序代码做出的必要的转换处理.宏有两个作用: 1.当遇到需要将程序某个特定的数量在程序中出现的所有实例通通加以修改时,程序只需改动一处即 ...
- Node.js 连接 MySQL 并进行数据库操作
Node.js 连接 MySQL 并进行数据库操作 按照这篇操作mysql的指引,我远程操作了我另一台电脑的mysql数据库. var mysql = require('mysql'); var c ...
- 基于jQuery的AJAX和JSON的实例
通过jQuery内置的AJAX功能,直接访问后台获得JSON格式的数据,然后通过jQuer把数据绑定到事先设计好的html模板上,直接在页面上显示. 我们先来看一下html模板: ...
- memcache保存php的session 解决多服务器的session问题
PECL :: Package :: memcache 2.1.1 版本的 Changelog 中有一条: - Added experimental session storage support. ...
- android学习——MeasureSpec介绍及使用
一.MeasureSpc类说明 SDK的介绍:MeasureSpc类封装了父View传递给子View的布局(layout)要求.每个MeasureSpc实例代表宽度或者高度 它有三种模式:①.UNSP ...
- Drupal如何实现类的自动加载?
Drupal通过spl_autoload_register()注册类加载器实现自动加载: function _drupal_bootstrap_database() { // ... .... spl ...
- Spring依赖注入 --- 简单使用说明
Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...
- 我的日常工具——gdb篇
我的日常工具——gdb篇 03 Apr 2014 1.gdb的原理 熟悉linux的同学面试官会问你用过gdb么?那好用过,知道gdb是怎么工作的么?然后直接傻眼... gdb是怎么接管一个进程?并且 ...
- Beginner’s Guide(开始者向导)
This guide gives a basic introduction to nginx and describes some simple tasks that can be done with ...