Java for LeetCode 004 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)).

解题思路:

由于要求时间复杂度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
                    + findKth(nums1, 0, nums2, 0,
                            (nums1.length + nums2.length) / 2 + 1) / 2;
    }
}
时间: 2024-08-10 15:05:11

Java for LeetCode 004 Median of Two Sorted Arrays的相关文章

LeetCode 004 Median of Two Sorted Arrays - Java

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,

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 004.Median_of_Two_Sorted_Arrays (Hard) 链接: 题目:https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/ 代码(github):https://github.com/illuz/leetcode 题意: 求

LeetCode.004 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)). 题意 有两个排序好的数组nums1 & nums2.找到这两个数组的中位数. 思路 令sum等于两个数组大小的和. 如果sum==1,返回这个数. 否则,如

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

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 arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

LeetCode OJ - 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 arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][JavaScript]Median of Two Sorted Arrays

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)). https://leetcode.com/problems/median-of-two-sorted

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并