LeetCode 4 : Median of Two Sorted Arrays ---- 两排序数组的中位数
来源:互联网 发布:指纹密码锁软件下载 编辑:程序博客网 时间:2024/05/16 11:29
原题链接: https://leetcode.com/problems/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)).
二:分析理解
对于数组个数总和是偶数还是奇数,假如是偶数4和奇数5,我们要分别找到2,3和3,那么我们统一一下,找到3,把3前面的也就是2记录下来,然后判断是奇数还是偶数,进而计算。
三:AC代码
class Solution{public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { double ans; int size1=nums1.size(); int size2=nums2.size(); bool isEven=((size1+size2)%2==0)?true:false;//标记是否是偶数 int breakPoint=(size1+size2)/2+1;//结束位置 int i=0; int j=0; int c=0;//记录是否到达中位数的坐标 2 while(i<size1||j<size2) { if((i<size1&&j==size2)||(i<size1&&j<size2&&nums1[i]<=nums2[j])) { c++; if(c==breakPoint) { if(isEven)//是偶数 { ans+=nums1[i]; ans/=2.0; } else ans=nums1[i]; break; } ans=nums1[i++]; } else { c++; if(c==breakPoint) { if(isEven) { ans+=nums2[j]; ans/=2.0; } else ans=nums2[j]; break; } ans=nums2[j++]; } } return ans; }};
返回LeetCode 题解目录
1 0
- LeetCode 4 : Median of Two Sorted Arrays ---- 两排序数组的中位数
- LeetCode(40) Median of Two Sorted Arrays (两排序数组中位数)
- Leetcode刷题记—— 4. Median of Two Sorted Arrays(两有序数组的中位数)
- leetcode Median of Two Sorted Arrays java 两个排序数组的中位数
- leetcode解题之4. Median of Two Sorted Arrays java (两个排序数组的中位数)
- [Leetcode #4]Median of Two Sorted Arrays 计算两个有序数组的中位数
- 【leetcode】 4Median of Two Sorted Arrays求两个数组的中位数
- [leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数
- 两个有序数组的中位数 Median of Two Sorted Arrays
- Median of Two Sorted Arrays 两个数组的中位数
- 【LeetCode-面试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
- 【数组】两个排序数组的中位数Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays,找出两个排好序的数组的中位数
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- LeetCode | Median of Two Sorted Arrays(两个数组的中位数)
- LeetCode OJ 之 Median of Two Sorted Arrays(两个有序数组的中位数)
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- group by 和 having的理解
- 数据结构——树 总结
- apt-get和aptitude
- 常见的虚拟机镜像下载
- RxJava线程控制 —— Scheduler (一)
- LeetCode 4 : Median of Two Sorted Arrays ---- 两排序数组的中位数
- 2.C++--const限定符
- SharedPreferences详解
- jq控制输入手机号码格式显示为3-4-4
- ffmpeg 编码h264 profile如何设置为baseline的问题
- 链表List
- 搞明白IRP这个东东了
- 深入理解电容器的等效串联电阻(ESR),多个小电容并联取代大电解电容的作用
- 如何选择外墙瓷砖