题解 | #牛的体重排序#
牛的体重排序
https://www.nowcoder.com/practice/1afd5afef2aa49a8a39b63bb9d2821f9
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weightsA int整型vector * @param weightsB int整型vector * @return double浮点型 */ double findMedianSortedArrays(vector<int>& weightsA, vector<int>& weightsB) { // write code here int m = weightsA.size(); int n = weightsB.size(); vector<int> merge; int i=0,j=0; while(i<m && j<n){ if(weightsA[i] <= weightsB[j]){ merge.push_back(weightsA[i]); i++; }else{ merge.push_back(weightsB[j]); j++; } } while(i<m){ merge.push_back(weightsA[i]); i++; } while(j<n){ merge.push_back(weightsB[j]); j++; } int mid = (m+n)/2; if((m+n)%2!=0){ return merge[mid]; }else{ return (merge[mid]+merge[mid-1])/2; } } };