LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE

习题如下:

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, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5
最开始想到的解法比较大众,两个数组的中位数不外乎就是对两个数组合并排序后的中位数,如果m+n是偶数,那么中位数就是合并后的第(m+n)/2+1个元素,如果m+n是奇数,那么中位数就是第(m+n)/2-1和第(m+n)/2+1个数相加除以2.那么问题就变成把两个有序数组合并,当合并到第(m+n)/2个元素的时候返回那个数即可,而且不用把结果数组存起来。但是注意时间复杂度是O(m+n),不符合要求

优化算法:问题等价于求两个array的第k=(m+n)/2(假设m和n分别是两个数组的元素个数)大的数是多少。基本思路是每次通过查看两个数组的第k/2大的数(假设是A[k/2],B[k/2]),如果两个A[k/2]=B[k/2],说明当前这个数即为两个数组剩余元素的第k大的数,如果A[k/2]>B[k/2], 那么说明B的前k/2个元素都不是我们要的第k大的数,反之则排除A的前k/2个,如此每次可以排除k/2个元素,最终k=1时即为结果。总的时间复杂度为O(logk),空间复杂度也是O(logk),即为递归栈大小。在这个题目中因为k=(m+n)/2,所以复杂度是O(log(m+n))。

具体代码如下:
{
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int m=nums1.length;
        int n=nums2.length;
        int total=m+n;

        if(total%2 == 0){
            return (double)findKth(nums1,nums2,0,m-1,0,n-1,(m+n)/2+1);
        }else{
            double temp1= findKth(nums1,nums2,0,m-1,0,n-1,(m+n)/2-1);
            double temp2= findKth(nums1,nums2,0,m-1,0,n-1,(m+n)/2+1);
            return (temp1+temp2)/2;
        }

    }

    public int findKth(int[] A,int[] B,int start_A,int end_A,int start_B,int end_B,int K){
        int lengthA=end_A-start_A+1;
        int lengthB=end_B-start_B+1;
        if(lengthA > lengthB)
            return findKth(B,A,start_B,end_B,start_A,end_A,K);
        if(lengthA==0)
            return B(start_B+K-1);
        if(k==1)
            return Math.min(A[k],B[k]);

        Pos_A=Math(K/2,m);
        Pos_B=K-Pos_A;
        if(A[Pos_A]==B[Pos_B])
            return A[start_A+Pos_A];
        else if(A[Pos_A] > B[Pos_B])
            return findKth(A,B,start_A,end_A,start_B+Pos_B,end_B,K-Pos_B);
        else
            return findKth(A,B,start_A+Pos_A,end_A,startB,end_B,K-Pos_A)

    }
}

  

要注意的几个点:

1.采用找两个数组第K个大小的值时,为了防止越界,需保证m<n。

2.有可能m<K,这里需要增加临界判断,以防止数组越界。

时间: 2024-10-26 21:20:39

LeetCode-刷题 Median of Two Sorted Arrays的相关文章

【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归

leetcode.C.4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. 1 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { 2 int numsSize = nums1Size + nums2Size; 3 int *nums = (int*)malloc(sizeof(int)*numsSize); 4 do

LeetCode第四题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)). example: nums1 = [1, 3] nums2 = [2] The median is 2.0 简单来说,这道题的要求就是求两个数组的中位数,因

Leetcode Array 4 Median of Two Sorted Arrays

做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法了.刚开始没啥思路,就用暴力的方法,用双层循环遍历的一下两个已经排好序的数组 ,在中间位置停止找道中位数.这样时间复杂度是肯定不能满足题目要求的,但是程序测试还是过了. 苦于自己没有思路,又不甘心就这样水过一道题,还是搜了一下博客,膜拜了一下大神.最好的方法是将中位数  -- 两个数组数据排好序之后

leetcode -- Algorithms -- 4_ Median of Two Sorted Arrays

00 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)). class Solution(object): def findMedianSortedArrays(self, nums1, nums2): new

【Leetcode】4. 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)). 解题思路: 这道题个人觉得是很难的,要想解出这个题,就必须了解一下几点: 1.求一列长度为len的数字的中位数,其实也可以被看做是求第len/2

【LeetCode OJ】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/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)). 解题思路:将两个有序数

LeetCode Problem 4.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)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode OJ 4. 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)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,