leetcode 4.Median of Two Sorted Arrays

原创 2016年06月01日 17:52:44

题目

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

LeetCode 4 Median of Two Sorted Arrays

翻译有两个给定的排好序的数组nums1和nums2,其大小分别为m和n。 找出这两个已排序数组的中位数。 总运行时间的复杂度应该是O(log(m+n))。原文There are two sorted ...
  • NoMasp
  • NoMasp
  • 2015-09-17 16:13
  • 2065

Leetcode刷题记—— 4. Median of Two Sorted Arrays(两有序数组的中位数)

一、题目叙述: There are two sorted arrays nums1 and nums2 of size m...

LeetCode (4)Median of Two Sorted Arrays

(4)Median of Two Sorted Arrays题目:两个已经排好序的数组nums1和nums2,大小分别为m和n,求两个已排数组中位数。总运行时间复杂度应该是O(log(m+n))。用例...

【leetcode】4. Median of Two Sorted Arrays

Difficulty:Hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Fi...

LeetCode 4. Median of Two Sorted Arrays

题目描述 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media...

[LeetCode][4]Median of Two Sorted Arrays解析 -Java实现

Q: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the...

LeetCode 题目 4. Median of Two Sorted Arrays

题目原址 :点击打开链接   题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively...

leetcode #4: Median of Two Sorted Arrays

题目链接: Median of Two Sorted Arrays 这道题坑还是有点多的,刚看到的时候第一反应是:这不就是归并排序....还是图样啊。所以先这样提交了: class Sol...

Leetcode 4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays Total Accepted: 86265 Total Submissions: 471864 Difficulty: Hard ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)