一起刷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] 任意的一个元素
(其实这个想法,和我之前想到的那个还是蛮相似的。。。)。用递归很好实现,代码是学习别人的,感觉写的很好,比自己的写的好。
学习了,感觉自己代码实现能力还是不太好,要加强。
-----------------------------------------------------------------------------------------------------------------------------------
代码如下:
double f(int * nums1,int m,int *nums2,int n,int k) {
if(m > n) {
return f(nums2,n,nums1,m,k);
}
if(m == ) {
return nums2[k - ];
}
if(k == ) {
if(nums1[] < nums2[]) {
return nums1[];
}else {
return nums2[];
}
}
int pa,pb;
if(k/ < m) {
pa = k/;
pb = k - pa;
}else {
pa = m;
pb = k -pa;
}
if(nums1[pa - ] < nums2[pb - ]) {
return f(nums1 + pa,m - pa,nums2 , n, k - pa);
}else if(nums1[pa - ] > nums2[pb - ]) {
return f(nums1 ,m ,nums2 + pb, n - pb, k - pb);
}else {
return nums1[pa - ];
}
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int total = nums1Size + nums2Size;
if(total % == ){
return ( f(nums1,nums1Size,nums2,nums2Size,total/) + f(nums1,nums1Size,nums2,nums2Size,total/ + ) ) / ;
}else {
return f(nums1,nums1Size,nums2,nums2Size,total/ + );
}
}
一起刷LeetCode4-Median of Two Sorted Arrays的更多相关文章
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode4 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- leetcode4 Median of Two Sorted Arrays学习记录
学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
- 刷题4. Median of Two Sorted Arrays
一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- 简单说说PHP优化那些事
我们在编写程序时,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行速度与最 ...
- 内核MKDEV(MAJOR, MINOR)宏
版本:linux-2.6.24.4宏: MKDEV(MAJOR, MINOR); 说明: 获取设备在设备表中的位置. MAJOR 主设备号 MINOR 次设 ...
- jQuery $.post $.ajax用法
jQuery $.post $.ajax用法 jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求 参数: url (Stri ...
- Effective C++学习笔记 条款02:尽量以const,enum,inline替换 #define
尽量使用const替换 #define定义常量的原因: #define 不被视为语言的一部分 宏定义的常量,预处理器只是盲目的将宏名称替换为其的常量值,导致目标码中出现多分对应的常量,而const定义 ...
- JS中令人发指的valueOf方法介绍
彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.prototype.valueOf ( ) When the ...
- Android远程图片获取和本地缓存
对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...
- UVa 11752 (素数筛选 快速幂) The Super Powers
首先有个关键性的结论就是一个数的合数幂就是超级幂. 最小的合数是4,所以枚举底数的上限是pow(2^64, 1/4) = 2^16 = 65536 对于底数base,指数的上限就是ceil(64*lo ...
- BZOJ2111: [ZJOI2010]Perm 排列计数
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2111 题意:一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2< ...
- malloc、free的使用
一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...
- 【C#学习笔记】smtp发邮件
using System; using System.Net; using System.Net.Mail; using System.Text; namespace ConsoleApplicati ...