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)).

解题思路:

由于要求时间复杂度O(log (m+n))所以几乎可以肯定是递归和分治的思想。

《算法导论》里有找两个数组第K小数的算法,时间复杂度为O(log(m+n)),所以直接调用即可

参考链接:http://blog.csdn.net/yutianzuijin/article/details/11499917/

Java参考代码:

public class Solution {
public static double findKth(int[] nums1, int index1, int[] nums2,
int index2, int k) {
if (nums1.length - index1 > nums2.length - index2)
return findKth(nums2, index2, nums1, index1, k);
if (nums1.length - index1 == 0)
return nums2[index2 + k - 1];
if (k == 1)
return Math.min(nums1[index1], nums2[index2]);
int p1 = Math.min(k / 2, nums1.length - index1), p2 = k - p1;
if (nums1[index1 + p1 - 1] < nums2[index2 + p2 - 1])
return findKth(nums1, index1 + p1, nums2, index2, k - p1);
else if (nums1[index1 + p1 - 1] > nums2[index2 + p2 - 1])
return findKth(nums1, index1, nums2, index2 + p2, k - p2);
else
return nums1[index1 + p1 - 1];
} static public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if ((nums1.length + nums2.length) % 2 != 0)
return findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2 + 1);
else
return findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2)
/ 2.0
+ findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2 + 1) / 2.0;
}
}

C++实现如下:

 #include<vector>
#include<algorithm>
using namespace std;
class Solution {
private:
double findKth(vector<int> nums1, int index1, vector<int> nums2, int index2, int k) {
if (nums1.size() - index1 > nums2.size() - index2) {
swap(nums1, nums2);
swap(index1,index2);
}
if (nums1.size() - index1 == )
return nums2[index2 + k - ];
if (k == )
return min(nums1[index1], nums2[index2]);
int p1 = min(k / , (int)nums1.size() - index1), p2 = k - p1;
if (nums1[index1 + p1 - ] < nums2[index2 + p2 - ])
return findKth(nums1, index1 + p1, nums2, index2, k - p1);
else if (nums1[index1 + p1 - ] > nums2[index2 + p2 - ])
return findKth(nums1, index1, nums2, index2 + p2, k - p2);
else
return nums1[index1 + p1 - ];
} public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if ((nums1.size() + nums2.size()) &)
return findKth(nums1, , nums2, ,
(nums1.size() + nums2.size()) / + );
else
return findKth(nums1, , nums2, ,(nums1.size() + nums2.size()) / )/ 2.0
+ findKth(nums1, , nums2, ,(nums1.size() + nums2.size()) / + ) / 2.0;
}
};

【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays的更多相关文章

  1. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. Java 集合类详解(含类图)

    0.参考文献 此图中蓝色为抽象类.深红色表示接口(Arrays除外).绿色表示具体容器类 1.java集合类图 1.1 1.2 上述类图中,实线边框的是实现类,比如ArrayList,LinkedLi ...

  2. BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)

    竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...

  3. Bsoj 1322 第K小数

    第K小数 Description 现在已有N个整数,你有以下三种操作: 1 A:表示加入一个值为A的整数: 2 B:表示删除其中值为B的整数: 3 K:表示输出这些整数中第K小的数: Input 第一 ...

  4. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  5. Eclipse开发Android程序如何在手机上运行

    android开发不论是在真机上调试还是最终发布到真机上都非常简单,过程如下: 1.安装usb驱动 手机要能与电脑相连,当然要安驱动了.效果就是你插入手机,电脑显示驱动已识别.驱动安装的官方教程:ht ...

  6. linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg)

     linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg) 2013-11-10 16:51:14 分类: 系统运维 为什么要写这篇文章? 答:通过常规的三大步(./confi ...

  7. poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))

    这里不在详细介绍威佐夫博弈论 简单提一下 要先提出一个名词“奇异局势”,如果你面对奇异局势则必输 奇异局势前几项(0,0).(1,2).(3,5).(4,7).(6,10).(8,13).(9,15) ...

  8. ifconfig 工具

    ifconfig 工具 ifconfig 命令常用格式: 格式:ifconfig显示当前激活的网络接口信息. 格式:ifconfig {INTERFACE}显示指定网络接口的信息.比如:eth0, e ...

  9. webservice和restful的区别

    REST是一种架构风格,其核心是面向资源,REST专门针对网络应用设计和开发方式,以降低开发的复杂性,提高系统的可伸缩性.REST提出设计概念和准则为: 1.网络上的所有事物都可以被抽象为资源(res ...

  10. 闲来无事,用Java的软引用写了一个山寨的缓存

    闲来无事,用Java的软引用写了一个山寨的缓存 博客分类: java基础 众所周知java中的引用分为 StrongReference.SoftReference.WeakReference.Phan ...