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

代码

public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    	double res = 0;
    	int len1 = nums1.length;
    	int len2 = nums2.length;
    	int len = len1 + len2;
    	int[] total = new int[len];
    	for(int i=0;i<len1;i++){
    		total[i] = nums1[i];
    	}
    	for(int j=0;j<len2;j++){
    		total[len1+j] = nums2[j];
    	}
    	
    	Arrays.sort(total);
    	if(len%2==0){
    		res = (double) (total[len/2-1] + total[len/2])/2;
    	}else{
        	res = total[len/2];    		
    	}

    	return res;
    }
}


阅读更多
文章标签: leetcode
个人分类: LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

leetcode 4.Median of Two Sorted Arrays

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭