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

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

简单来说就是求两个已经排好序的数组的中位数

代码

1. 先合并两个数组进行排序,然后求中位数
   public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //先给两个数组进行排序
        int[] datas = new int[nums1.length + nums2.length];
        int i = 0;
        int k = 0;
        int index = 0;
        while (i=nums2.length) {
                datas[index++] = nums1[i++];
                continue;
            }
            if(i>=nums1.length) {
                datas[index++] = nums2[k++];
                continue;
            }
            if(nums1[i]

第二种方法的逻辑要多想

你可能感兴趣的:(4. Median of Two Sorted Arrays)