题解 | #在两个长度相等的排序数组中找到上中位数#
在两个长度相等的排序数组中找到上中位数
https://www.nowcoder.com/practice/6fbe70f3a51d44fa9395cfc49694404f
import java.util.*; public class Solution { /** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here if (arr1 == null || arr2 == null) { return -1; } int len1 = arr1.length; int len2 = arr2.length; int mid = (len1 + len2) % 2 == 0 ? (len1 + len2) / 2 : (len1 + len2) / 2 + 1; int i = 0; int j = 0; int k = 0; int res = 0; while (i < len1 && j < len2) { if (arr1[i] <= arr2[j]) { res = arr1[i++]; } else { res = arr2[j++]; } if (k == mid - 1) { return res; } k++; } if (i < len1) { return arr1[mid - len2]; } return arr2[mid - len1]; } }