import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param herd1 int整型一维数组
     * @param herd2 int整型一维数组
     * @param herd3 int整型一维数组
     * @return double浮点型
     */
    public double findMedianSortedArray (int[] herd1, int[] herd2, int[] herd3) {
        // write code here
        int[] nums1 = new int[herd1.length + herd2.length + herd3.length];
        System.arraycopy(herd1, 0, nums1, 0, herd1.length);
        System.arraycopy(herd2, 0, nums1, herd1.length, herd2.length);
        System.arraycopy(herd3, 0, nums1, herd1.length + herd2.length, herd3.length);

        Arrays.sort(nums1);
        int n = nums1.length;
        if (n % 2 == 0) {
            return (nums1[n / 2] + nums1[n / 2 - 1]) / 2.0;
        } else {
            return nums1[n / 2];
        }
    }
}

编程语言是Java。

该题考察的知识点是数组操作和排序。

代码的文字解释如下:

  1. 使用System.arraycopy()方法将herd1herd2herd3中的元素依次拷贝到nums1数组中,保持原有顺序。
  2. 使用Arrays.sort()方法对nums1数组进行排序,使其按照升序排列。
  3. 获取排序后的数组nums1的长度n
  4. 判断数组长度是否为偶数,如果是,则返回数组中位数为中间两个数的平均值(nums1[n / 2] + nums1[n / 2 - 1]) / 2.0
  5. 如果数组长度为奇数,则返回数组中位数为中间的那个数nums1[n / 2]