一起刷LeetCode4-Median of Two Sorted Arrays

  实验室太吵了。。。怎么办啊。。。

--------------------------------------------------------------------------------------------------------------------

Median of Two Sorted Arrays

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)).

【题意】:给你两个已经排好序的数组,求中位数,要求时间复杂度为:O(log(m+n))。

【心路历程】:看完这道题直接的想法就是用merge 的方法对两个数组合并排序,但是时间复杂度为:O((m+n))。

因为要求的时间复杂度为O(log(m+n)),所以很自然会想去用二分求解。于是,我最开始的思路是分别对两个数组进行二分,

然后进行比较,如果A > B,则去掉B的小于N/2的部分,去掉A的大于M/2的部分。如果A < B,则去掉A的小于M/2的部分,

去掉B的大于M/2的部分。如果 A = B ,则返回中间值。后来发现这个方法有很多不妥之处,自己实现时也是感觉有问题。

于是又开始想,发现自己确实没啥思路,就无耻的度娘了一下,发现这题可以转化成求最K值问题。原来在《剑指OFFER》上看过

最k值问题,但是却忘得一干二净了。。。

这题就是最K值问题,两个数组分别取第K/2个元素进行比较。如果A[K/2] < B[K/2] ,则忽略掉A[k/2]之前的所有元素;同理如果

A[K/2] > B[K/2] ,则忽略掉B[K/2]之前的所有元素;如果A[K/2] == B[K/2] ,则返回A[K/2] 或 B[K/2] 任意的一个元素

(其实这个想法,和我之前想到的那个还是蛮相似的。。。)。用递归很好实现,代码是学习别人的,感觉写的很好,比自己的写的好。

学习了,感觉自己代码实现能力还是不太好,要加强。

-----------------------------------------------------------------------------------------------------------------------------------

代码如下:

 1 double f(int * nums1,int m,int *nums2,int n,int k) {
 2     if(m > n) {
 3         return f(nums2,n,nums1,m,k);
 4     }
 5     if(m == 0) {
 6         return nums2[k - 1];
 7     }
 8     if(k == 1) {
 9         if(nums1[0] < nums2[0]) {
10             return nums1[0];
11         }else {
12             return nums2[0];
13         }
14     }
15     int pa,pb;
16     if(k/2 < m) {
17         pa = k/2;
18         pb = k - pa;
19     }else {
20         pa = m;
21         pb = k -pa;
22     }
23     if(nums1[pa - 1] < nums2[pb - 1]) {
24         return f(nums1 + pa,m - pa,nums2 , n, k - pa);
25     }else if(nums1[pa - 1] > nums2[pb - 1]) {
26         return f(nums1 ,m ,nums2 + pb, n - pb, k - pb);
27     }else {
28         return nums1[pa - 1];
29     }
30 }
31
32 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
33
34     int total = nums1Size + nums2Size;
35     if(total % 2 == 0){
36         return ( f(nums1,nums1Size,nums2,nums2Size,total/2) + f(nums1,nums1Size,nums2,nums2Size,total/2 + 1) ) / 2;
37     }else {
38         return f(nums1,nums1Size,nums2,nums2Size,total/2 + 1);
39     }
40 }
时间: 2024-08-27 00:52:14

一起刷LeetCode4-Median of Two Sorted Arrays的相关文章

LeetCode4 Median of Two Sorted Arrays Java实现

描述: 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)). 两个排好序的的数组找中位数,时间复杂度要求 O(log (m+n)). 如果把两个数组合并,排序再找中位数,显然时间复杂度是O(nlogn). 问题转换为求两个数组

leetcode4. Median of Two Sorted Arrays

题目大意:找到两个排序数组的中位数. 水了一发,将两个数组合并为一个数组,直接输出中位数了.(可见leetcode好像并没有对时间进行控制). public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int []num = new int [nums1.length+nums2.length]; int i; for(i = 0;i < nums1.length;i++) n

周刷题第二期总结(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

刷题4. Median of Two Sorted Arrays

一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题. 性能只有42%的样子,内存占用太多.还需要进一步优化!!! 二.这个题目,我自己实现 提交了2次: 第1次: Wrong Answer 第2次:终于对了 下面是我的完整代码实现,需要的拿去: #include<iostream> #include<vector> using nam

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

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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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)). 这道题的