Median of Two Sorted Arrays(hard)

There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

我的解答:

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        vector<int> neo;
        double outcome=0 ;
        int a = nums1.size() + nums2.size();
        if (a % 2 != 0) {
            int current = 0;
            int b = 0;
            int c = 0;
            while (b != nums1.size() || c != nums2.size()) {
                if (b == nums1.size() ) {
                    neo.push_back(nums2[c]);
                    current++;
                    c++;
                }
                else if (c == nums2.size()) {
                    neo.push_back(nums1[b]);
                    current++;
                    b++;
                }
                else {
                    if (nums1[b] <= nums2[c]){
                        neo.push_back(nums1[b]);
                        b++;
                        current++;
                    }
                    else {
                        neo.push_back(nums2[c]);
                        c++;
                        current++;
                    }
                }
                if (current == (a + 1) / 2) {
                    outcome = neo[current - 1];
                    break;
                }
            }
        }
        else {
            int current = 0;
            int b = 0;
            int c = 0;
            while (b != nums1.size() || c != nums2.size()) {
                if (b == nums1.size() ) {
                    neo.push_back(nums2[c]);
                    current++;
                    c++;
                }
                else if (c == nums2.size()) {
                    neo.push_back(nums1[b]);
                    current++;
                    b++;
                }
                else {
                    if (nums1[b] <= nums2[c]) {
                        neo.push_back(nums1[b]);
                        b++;
                        current++;
                    }
                    else {
                        neo.push_back(nums2[c]);
                        c++;
                        current++;
                    }
                }
                if (current == (a + 2) / 2) {
                    double x = neo[current - 1];
                    double y = neo[current - 2];
                    outcome = (x + y) / 2;
                    break;
                }
            }
        }
        return outcome;
    }
};

这里面主要是用到上学期数据结构学到的方法,就是用两个int变量分别标记两个向量的当前位置,判断哪个比较小,小的插入到新的向量中,同时所在的向量的位置标记加1,若一个向量已经插入完毕,那么重复插入剩下的那个向量的所有元素,这样能将两个排列好的向量合并成一个排列好的向量,由于题目只要求中位数,因此只需要插入的个数达到总元素数的一半即可得到所求(此处需要分开总数为奇数和偶数两种情况考虑),这也是循环终止的条件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值