题解 | #多数组中位数#
多数组中位数
https://www.nowcoder.com/practice/b6bb0bce88894108bfc23e9b7b012420
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr1 int整型一维数组
* @param arr2 int整型一维数组
* @return int整型
*
*/
public int getUpMedian (int[] arr1, int[] arr2) {
// write code here
int totalSteps = (arr1.length + arr2.length + 1) / 2;
int p1 = 0, p2 = 0, ans = 0, steps = 0;
while (p1 < arr1.length && p2 < arr2.length && steps < totalSteps) {
if (arr1[p1] < arr2[p2]) {
ans = arr1[p1];
p1++;
} else {
ans = arr2[p2];
p2++;
}
steps++;
}
while (p1 < arr1.length && steps < totalSteps) {
ans = arr1[p1];
p1++;
steps++;
}
while (p2 < arr2.length && steps < totalSteps) {
ans = arr2[p2];
p2++;
steps++;
}
return ans;
}
}
#我的实习求职记录#
查看9道真题和解析