LeetCode Median of Two Sorted Arrays 排序

2022/7/10 6:20:25

本文主要是介绍LeetCode Median of Two Sorted Arrays 排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be \(O(\log (m+n))\).

Solution

简单排序即可:

点击查看代码
class Solution {
private:
    vector<int> vc;
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        for(int i=0;i<nums1.size();i++)vc.push_back(nums1[i]);
        for(int i=0;i<nums2.size();i++)vc.push_back(nums2[i]);
        sort(vc.begin(),vc.end());
        if(vc.size()==0)return 0;
        if(vc.size()%2){
            return 1.0*vc[(vc.size()-1)/2];
        }
        else{
            return 0.5*(vc[vc.size()/2]+vc[vc.size()/2-1]);
        }
    }
};


这篇关于LeetCode Median of Two Sorted Arrays 排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程