题解 | #在两个长度相等的排序数组中找到上中位数#
在两个长度相等的排序数组中找到上中位数
http://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 int n = arr1.length+arr2.length; int k =0;//上中位数下标 if(n%2==0){ k=n/2; }else{ k=n/2+1; } //序号 int idx=0; //答案 int ans = 0; //arr1、arr2下标 int i1=0,i2=0; while(idx!=k){ if(arr1[i1]<=arr2[i2]){ ans = arr1[i1]; i1++; }else{ ans = arr2[i2]; i2++; } idx++; } return ans; } }