一起刷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.排序法 时间复 ...
随机推荐
- [Unity菜鸟] Final IK
由于本人英文较烂,边翻译用户手册边学习. 用户手册 IK Components Final IK 包含许多强大高速的IK组件 Aim AimIK solver是一个对CCD算法(cyclic co ...
- ARM CPU与Intel x86 CPU性能比较
Qualcomm ARM CPU与Intel x86 CPU性能比较 随着移动互联网时代的到来,Qualcomm(高通).Texas Instruments(德州仪器)等基于ARM架构的CPU受到越来 ...
- BCB一个问过100遍啊100遍的问题
一个问过100遍啊100遍的问题作者: ---------- ,如转载请保证本文档的完整性,并注明出处.欢迎光临 C++ Builder 研究, http://www.ccrun.com/doc/go ...
- Netstat 命令
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
- 【Spring】Redis的两个典型应用场景--good
原创 BOOT Redis简介 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化. ...
- Use Eclipse to develop groovy[docs.codehaus.org]
http://docs.codehaus.org/display/GROOVY/Install+Groovy-Eclipse+Plugin http://docs.codehaus.org/displ ...
- ios7 webapp touch bug
// ios7 touchstart bug if(navigator.userAgent.indexOf("iPhone OS 7") != -1){ var startX = ...
- 重写hashCode()的方法
重写hashCode()方法的基本规则: 1.在程序运行过程中,同一个对象多次调用hashCode()方法应该返回相同的值 2.当两个对象通过equals()方法比较返回true时,这两个对象的has ...
- $.post()
定义和用法 post() 方法通过 HTTP POST 请求从服务器载入数据. jQuery.post(url,data,success(data, textStatus, jqXHR),dataTy ...
- Android 按键消息处理Android 按键消息处理
在android系统中,键盘按键事件是由SystemServer服务来管理的:然后在以消息的形式分发给应用程序处理.产生键盘按键事件则是有Linux kernel的相关驱动来实现. 键盘消息有别于其他 ...