码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 4. Median of Two Sorted Arrays

时间:2019-01-22 12:22:34      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:turn   find   cut   solution   while   EDA   public   and   答案   

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

You may assume nums1 and nums2 cannot be both empty.

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

第一次没有看答案写出了faster than 99.3%的answer。。不过debug了很多很多次
但是我的time是O(m+n),不符合题意。
class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int l = nums1.length + nums2.length;
        int[] mer = new int[l];
        int i = 0;
        int j = 0;
        int i1 = 0;
        int j1 = 0;
        if(nums1.length == 0) {
            mer = nums2;
        } else if(nums2.length == 0) {
            mer = nums1;
        }else {
            for(int k = 0; k<l; k++) {
                if(i1==0 && j1==0){
                    if(nums1[i]<=nums2[j]) {
                        mer[k] = nums1[i];
                        if(i==nums1.length-1){i1 = 1;}
                        i++;
                    }else{
                        mer[k] = nums2[j];
                        if(j==nums2.length-1){ j1 = 1;}
                        j++;
                    }
                }else if(i1 == 1){
                    mer[k] = nums2[j];
                    if(j<nums2.length-1) j++;
                }else if(j1 == 1){
                    mer[k] = nums1[i];
                    if(i<nums1.length-1) i++;
                }
            }
        }
        double r;
        if(l%2 == 0) {
            r = ((double)mer[l/2-1]+(double)mer[l/2])/2.0;
        } else {
            r = mer[(l-1)/2];
        }
        return r;
    }
}

 

正确答案:

cut + binary search

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n1 = nums1.length;
        int n2 = nums2.length;
        if(n1<n2) return  findMedianSortedArrays(nums2,nums1);
        
        int lo = 0, hi = 2*n2;
        while(lo<=hi) {
            int md2 = (lo+hi) / 2;
            int md1 = n1 + n2 - md2;
            double l1 = (md1 == 0)? - Math.pow(2,31) : nums1[(md1-1)/2]; //java里面乘方不能用^ !!
            double l2 = (md2 == 0)? - Math.pow(2,31) : nums2[(md2-1)/2];
            double r1 = (md1 == 2*n1)? Math.pow(2,31)-1 : nums1[md1/2];
            double r2 = (md2 == 2*n2)? Math.pow(2,31)-1 : nums2[md2/2];
             
            if(l1>r2) {
                lo = md2+1;
            }else if(l2>r1) {
                hi = md2-1;
            }else {
                return (Math.max(l1,l2)+Math.min(r1,r2))/2;
            }
        }
        return -1;
    }

 

leetcode 4. Median of Two Sorted Arrays

标签:turn   find   cut   solution   while   EDA   public   and   答案   

原文地址:https://www.cnblogs.com/jamieliu/p/10301607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!