Median of Two Sorted Arrays(Java)

求2个数组的中位数

方法很多

但是时间复杂度各异

1利用数组copy方法先融合两个数组,然后排序,找出中位数

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class _004MedianofTwoSortedArrays {

    public static void main(String[] args)
    {
        int a[]={2,3,5,9};
        int b[]={1,4,7,10,11};
        System.out.println(findMedianSortedArrays(a,b));
    }

     public static double findMedianSortedArrays(int A[], int B[])
     {
         int[] d3 = new int[A.length + B.length];
         System.arraycopy(A, 0, d3, 0, A.length);
         System.arraycopy(B, 0, d3, A.length, B.length);
         Arrays.sort(d3);
         System.out.println(Arrays.toString(d3));
         return d3.length%2!=0?d3[d3.length/2]/1.0:(d3[d3.length/2-1]+d3[d3.length/2])/2.0;

      }

}

2是循环判断插入新数组中 等于说不用内置copy方法自己写一个替代

 1 import java.util.Arrays;
 2
 3
 4 public class _004_2Median {
 5
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         int a[]={2,34};
 9         int b[]={1,4,9};
10         System.out.println(findMedianSortedArrays(a,b));
11     }
12
13     private static double findMedianSortedArrays(int[] a, int[] b) {
14         int k=a.length+b.length;
15         System.out.println(k);
16         int[] c=new int[k];
17         int i=0,j=0,m=0;
18         while(m!=k)
19         {
20             if(i==a.length)
21             {
22                 c[m]=b[j];
23                 j++;
24
25             }
26             else if(j==b.length)
27             {
28                 c[m]=a[i];
29                 i++;
30             }
31             else if(a[i]<b[j])
32             {
33                 c[m]=a[i];
34                 i++;
35
36             }
37             else
38             {
39                 c[m]=b[j];
40                 j++;
41
42
43             }
44             m++;
45         }
46
47         return k%2==0?(c[k/2-1]+c[k/2])/2.0:c[k/2]/1.0;
48
49     }
50
51 }

3第三种递归方法有待研究

时间: 2024-10-27 07:40:52

Median of Two Sorted Arrays(Java)的相关文章

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays 这道题确实有点难,想挺久,看别人答案也是不能一下子就明白.题目难度为Hard,原题如下: 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 004 Median of Two Sorted Arrays - Java

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,

LeetCode4 Median of Two Sorted Arrays Java实现

描述: There are two sorted arrays A and B 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)). 两个排好序的的数组找中位数,时间复杂度要求 O(log (m+n)). 如果把两个数组合并,排序再找中位数,显然时间复杂度是O(nlogn). 问题转换为求两个数组

4. Median of Two Sorted Arrays【leetcode】java,算法,中间值

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 media

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

LeetCode——Median of Two Sorted Arrays

There are two sorted arrays A and B 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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: There are two sorted arrays A and B 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】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22