刷题4. Median of Two Sorted Arrays

一、题目

Median of Two Sorted Arrays,具体请自行搜索。

这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂。

但要做到bug free也不难,最大的问题是性能问题。

性能只有42%的样子,内存占用太多。还需要进一步优化!!!

二、这个题目,我自己实现

提交了2次:

第1次: Wrong Answer

第2次:终于对了

下面是我的完整代码实现,需要的拿去:

#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        float f = 0;
        vector<int> res;
        int i=0,j=0;
        while(i<m && j<n){
            if(nums1[i]<nums2[j]){
                res.push_back(nums1[i]);
                i++;
            }else{
                res.push_back(nums2[j]);
                j++;
            }
        }
        while(i<m){
            res.push_back(nums1[i]);
            i++;
        }
        while(j<n){
           res.push_back(nums2[j]);
           j++;
        }

        if((m+n) %2 == 0){
            //总共有偶数个,取中间2个平均值
            f = res[(m+n)/2-1]+res[(m+n)/2];
            return f/2;
        }else{
            //找到中间值
            return res[(m+n)/2];
        }
    }
};

int main(){
    vector<int> v1 = {1,3};
    vector<int> v2 = {2};
    Solution s;
    cout<<s.findMedianSortedArrays(v1,v2)<<endl;

    v1 = {1,2};
    v2 = {3,4};
    cout<<s.findMedianSortedArrays(v1,v2)<<endl;
    return 0;
}

三、改进

我先思考一下...

原文地址:https://www.cnblogs.com/siweihz/p/12231449.html

时间: 2024-11-05 13:03:53

刷题4. Median of Two Sorted Arrays的相关文章

LeetCode 第二题,Median of Two Sorted Arrays

题目再现 There are two sorted arrays A and B 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)). 题意解析 题目意思是给两个大小为m,n的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

算法题之Median of Two Sorted Arrays

这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: 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)). Example 1: n

第四题:Median of Two Sorted Arrays

题目链接:题目链接 题意:两个排好序的数组,找到中位数,如果是奇数好办,如果是偶数找到最中间的两个求平均值. 这一题的本质其实就是第mid小的数. 这一题看到一种好的方法,我们令k=mid,对于两个数组,分别取前k/2数,如果A[k/2-1]比B[k/2-1]大,那么说明B前 k/2数肯定在k小数中,A的则不一定,则下面需要从A,B+k/2再次去寻找...反之对于B大的情况也是一样的,如果相 等,那就是这两个数(相等)了,随便返回一个都OK.(Ps:解释一些为什么是k/2-1,因为k是从1开始取

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A and B 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)). 解题思路:这道题要求两个已经排好

[LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. Have you met this question in a real interview? Example Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5. Given A=[1,2,3] and B=[4,5],

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

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)). 给定两个已

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B 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)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg