[LeetCode]4.Median of Two Sorted Arrays (hard)

标签: LeetCode
1人阅读 评论(0) 收藏 举报
分类:

Welcome To My Blog

4. Median of Two Sorted Arrays (hard)

给定两个有序数组,找中位数median,按照自己的思路AC了,用了挺长时间
需要学习基础算法
4.1.png
1. 心得:
+ 解题要形成自己的框架!
+ 考虑索引在数组中是否有效,同时也要考虑索引的实际含义
+ 对两个需要同时成立的大小约束A[i-1]

class Solution{
    public double findMedianSortedArrays(int[] A, int[] B){
        int m = A.length;
        int n = B.length;
        //1.如果m>n则调换数组顺序,使满足m≤n,对长度短的数组做二分,速度更快!
        if(m>n) return findMedianSortedArrays(B, A);
        //2.(边界)当A为空时找出B中的median即可,分奇偶讨论
        //没有A,B都为空的情况
        if (m == 0 && n%2==1) return B[(n+1)/2-1];
        if(m == 0 && n%2==0) return (B[n/2-1]+B[n/2])/2.0;
        int iMin = 0,iMax = m,p = (m+n+1)/2;// p是partial,代表i,j等式中固定不变的成分
        //3.通过循环实现二分法,时间复杂度是O(log(m+n))
        while(iMin <= iMax){
            int i = (iMin + iMax)/2;
            int j = p - i;
            //4.两个大小关系约束分别处理,会发现可以合并成两行
            //4.1 0<i<m时0<j<n或i==0时,0<j<n   此时B[j-1] A[i]都有意义
            if(((i>0 && i< m) ||i == 0) && B[j-1] > A[i]) iMin += 1;
            //4.2 0<i<m时0<j<n或i==m时j>0(j肯定小于n,因为j==n会导致只有左边部分)
            else if (((i>0 && i< m)||i == m) && A[i-1] > B[j]) iMax -= 1;
            //6. 进入到else说明对于0≤i≤m有B[j-1] < A[i] && A[i-1] < B[j],找出median即可
            else {
                // i==0
                if (i == 0 && j == n && m == n) return (A[0] + B[n - 1]) / 2.0;
                else if (i == 0 && j == n && m + 1 == n) return B[n - 1];
                else if (i == 0 && j < n && (m + n) % 2 == 1) return B[j - 1];
                else if (i == 0 && j < n && (m + n) % 2 == 0) return ( B[j-1]+Math.min(A[0], B[j])) / 2.0;
                // i == m
                else if (i == m && j == 0) return (A[m - 1] + B[0]) / 2.0;
                else if (i == m   && (m + n) % 2 == 1) return Math.max(A[m - 1], B[j - 1]);
                else if (i == m   &&(m + n) % 2 == 0) return (Math.max(A[m - 1], B[j - 1]) + B[j]) / 2.0;
                //  0 < i < m
                else if ((m + n) % 2 == 1) return Math.max(A[i - 1], B[j - 1]);
                else return (Math.max(A[i - 1], B[j - 1]) + Math.min(A[i], B[j])) / 2.0;
            }
        }
        //double函数的返回值,其实这里写任何double的数都可以,因为用不到
        return 0.0;
        }
    }
查看评论

leetcode: 4. Median of Two Sorted Arrays (java)

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目: There are two sorted arrays nu...
  • j754379117
  • j754379117
  • 2016-06-20 20:44:18
  • 738

《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

004. Median of Two Sorted Arrays[H]Median of Two Sorted ArraysH 题目 分析 思路1 思路2 预备知识 先解释下割 双数组找第k个元素 我...
  • hk2291976
  • hk2291976
  • 2016-04-09 21:24:06
  • 4845

leetcode之 median of two sorted arrays

这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetc...
  • yutianzuijin
  • yutianzuijin
  • 2013-09-10 00:15:50
  • 80243

leetcode04-Median of Two Sorted Arrays-python

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two...
  • summerdj
  • summerdj
  • 2016-03-31 15:11:13
  • 899

4.leetcode Median of Two Sorted Arrays(medium)[求两个数组的中位数]

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
  • qz530308783
  • qz530308783
  • 2016-08-23 16:21:55
  • 187

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

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-28 00:03:30
  • 5727

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1]...
  • gao1440156051
  • gao1440156051
  • 2016-06-21 11:11:03
  • 4850

LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)

原题网址:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an...
  • jmspan
  • jmspan
  • 2016-05-01 15:39:34
  • 618

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 t...
  • fly_yr
  • fly_yr
  • 2015-08-22 20:35:01
  • 1335

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...
  • sunao2002002
  • sunao2002002
  • 2015-06-01 23:00:44
  • 2994
    个人资料
    持之以恒
    等级:
    访问量: 1311
    积分: 431
    排名: 11万+
    文章存档