`
cozilla
  • 浏览: 94032 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Median of Two Sorted Arrays

 
阅读更多
Median of Two Sorted ArraysMar 28 '116215 / 31527

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

» Solve this problem

很久前就见过这题,当时随便一想,以为自己想到了logN的方法。

前几天又看到,打算写一下。发现之前的想法不靠谱啊!!

之前是想:每次找m/2, n/2处。然后比来比去搞。思路错了。

后来参考了:http://blog.csdn.net/zxzxy1988/article/details/8587244

 

 

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int total = n + m;
        
        if (total % 2 == 0) {
            return (findKth(A, m, B, n, total/2) + findKth(A, m, B, n, total / 2 + 1)) / 2;
        }
        else 
            return findKth(A, m, B, n, total / 2 + 1);
    }
    
    double findKth(int a[], int m, int b[], int n, int k) {
        if (m > n) return findKth(b, n, a, m, k);
        if (m == 0) return b[k-1];
        if (k == 1) return min(a[0], b[0]);
        
        int ia = min(k/2, m);
        int ib = k - ia;
        if (a[ia-1] < b[ib-1]) return findKth(a+ia, m - ia, b, n, k - ia);
        else  if (a[ia-1] > b[ib-1]) return findKth(a, m, b+ib, n - ib, k - ib);
        else return a[ia-1];
    }
};

 

 

 

分享到:
评论

相关推荐

    LeetCode4 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)). Java AC版本

    java-leetcode题解之Median of Two Sorted Arrays.java

    这个问题是在LeetCode上编号为4的难题,要求在O(log(min(m,n)))的时间复杂度内完成算法,其中m和n分别是两个已排序数组的长度。中位数的定义是:对于两个排序数组而言,当合并为一个排序数组后,位于中间位置的数即...

    leetcode无法登录-MedianOfTwoSortedArrays:双排序数组的中位数

    leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 不能都为空。 ...

    java-leetcode题解之004-Median-of-Two-Sorted-Arrays

    java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays

    程序员面试宝典LeetCode刷题手册

    4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...

    c语言-leetcode题解04-median-of-two-sorted-arrays.c

    本次提到的"C语言-leetcode题解04-median-of-two-sorted-arrays.c"文件,具体是关于如何使用C语言来解决LeetCode上的第4题——两个排序数组的中位数。中位数是统计学中的一种度量,它代表了一个数据集合中间位置的...

    leetcodepython001-algorithm:leetcode问题(cpp、java、python),书籍破解_the_coding

    leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...

    js-leetcode题解之4-median-of-two-sorted-arrays.js

    其中,LeetCode是一个流行的技术面试准备网站,它提供了一系列的编程题库供用户练习。本文将详细解读LeetCode中的一个经典算法问题:如何在一个合并的有序数组中找到中位数,通常称为“第4题:两个有序数组中的中位...

    lrucacheleetcode-leetcode:leetcode

    Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix ...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    Coding Interview In Java

    7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two ...

    leetcode分类-leetcode:leetcode问题的代码

    #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode分类-LeetCodeSrc:力扣

    leetcode分类LeetCodeSourceCodes###LeetCode1TowSum如果数组是有序的,O(n)的时间复杂度就可以解决了,现在是无序的,一开始要排一下序###LeetCode2MedianofTwoSortedArrays让用logn的时间复杂度,用了两个二分,...

    leetcode2-Leetcode:Leetcode_answer

    Median of Two Sorted Arrays JavaScript O(log (m+n)) O(1) Hard 7 Reverse Integer JavaScript O(n) O(1) Easy 9 Palindrome Number JavaScript O(n) O(1) Easy 19 Remove Nth Node From End of List JavaScript O...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    Median of Two Sorted Arrays 5 Longest Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove ...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    Median of Two Sorted Arrays 30.7% Hard 0005 Longest Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium ...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    LeetCode 刷题汇总1

    * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    Median of Two Sorted Arrays 36.3% 困难 5 Longest Palindromic Substring 27.6% 中等 6 ZigZag Conversion 45.6% 中等 7 Reverse Integer 33.2% 简单 8 String to Integer (atoi) 18.5% 中等 9 Palindrome Number ...

Global site tag (gtag.js) - Google Analytics