题解 | #牛的体重排序#

牛的体重排序

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
        for (int i = 0; i < weightsB.size(); ++i)
            weightsA.push_back(weightsB[i]);
        sort(weightsA.begin(), weightsA.end());
        int num = weightsA.size();
        int x = num / 2 - 1;
        if (num % 2)
            return weightsA[x + 1];
        else  
            return (weightsA[x] + weightsA[x + 1]) / 2.0;
    }
};

全部评论

相关推荐

包行:平时怎么刷算法题的哇,字节的手撕听说都很难
字节跳动工作体验
点赞 评论 收藏
分享
迷茫的大四🐶:你这个拿去投央国企吧,投私企包过不了的
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务