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. Nginx Installation、Configuration、Rreverse Proxy、Load Balancing Learning

    目录 . Nginx简介 . Nginx安装部署 . Nginx安全配置 . Nginx反向代理实践 . Nginx负载均衡实践 1. Nginx简介 0x1: Nginx的基本特性 Nginx(&q ...

  2. Rebar:Erlang构建工具

    Rebar是一款Erlang的构建工具,使用它可以方便的编译.测试erlang程序.内联驱动和打包Erlang发行版本. Rebar是一个独立的erlang脚本,所以使用Rebar发布程序非常简单,甚 ...

  3. Activity启动模式 及 Intent Flags 与 栈 的关联分析

     http://blog.csdn.net/vipzjyno1/article/details/25463457    Android启动模式Flags栈Task   目录(?)[+] 什么是栈 栈 ...

  4. linux下IPTABLES配置详解(转)

    如果你的IPTABLES基础知识还不了解,建议先去看看.开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况[ ~]# iptables -L -nChain I ...

  5. Chrome浏览器插件

    Chrome 布局 1. 修改Chrome Dock side Chrome 更多工具 -> 开发者工具 -> Customsize and Control Dev Tools

  6. manifest package

    http://www.android-doc.com/guide/topics/manifest/manifest-intro.html It names the Java package for t ...

  7. HttpClient教程

    2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...

  8. Bookmarklet

    学习 http://www.ruanyifeng.com/blog/2011/06/a_guide_for_writing_bookmarklet.html

  9. Unity-Tween

    1.GoKit 免费开源 AssetStore:https://www.assetstore.unity3d.com/en/#!/content/3663 下载地址:https://github.co ...

  10. WebService中方法的相关注意事项

    2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为 ...