题解 | #牛的体重排序#
牛的体重排序
https://www.nowcoder.com/practice/1afd5afef2aa49a8a39b63bb9d2821f9
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weightsA int整型一维数组 * @param weightsB int整型一维数组 * @return double浮点型 */ public double findMedianSortedArrays (int[] weightsA, int[] weightsB) { // write code here int m = weightsA.length, n = weightsB.length, i = 0, j = 0, k = 0; int[] weights = new int[m + n]; while (i < m && j < n) { if (weightsA[i] < weightsB[j]) weights[k++] = weightsA[i++]; else weights[k++] = weightsB[j++]; } while (i < m) weights[k++] = weightsA[i++]; while (j < n) weights[k++] = weightsB[j++]; int len = weights.length; return len % 2 == 0 ? (weights[len >> 1] + weights[(len >> 1) - 1]) /2.0d : weights[len / 2] * 1.0d; } }#归并排序#
线性表基础 文章被收录于专栏
链表、递归、栈