leetcode 4: Median of Two Sorted Arrays
来源:互联网 发布:微信商城h5源码下载 编辑:程序博客网 时间:2024/06/15 17:02
Use the idea of merge sort.
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m=nums1.size(); int n=nums2.size(); int i=0,j=0,curr=1; double res1,res2; while(i<m&&j<n) { if(curr==(m+n)/2) res1=min(nums1[i],nums2[j]); if(curr==(m+n)/2+1) { res2=min(nums1[i],nums2[j]); break; } if(nums1[i]<=nums2[j]) i++; else j++; curr++; } while(curr<=(m+n)/2+1) { if(curr==(m+n)/2) { if(i==m) res1=nums2[j]; else if(j==n) res1=nums1[i]; } if(curr==(m+n)/2+1) { if(i==m) res2=nums2[j]; else if(j==n) res2=nums1[i]; } if(i<m) i++; else if(j<n) j++; curr++; } if((m+n)%2) return res2; return (res1+res2)/2; }};
0 0
- Leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [leetcode 4] Median of Two Sorted Arrays
- LeetCode 4:《Median of Two Sorted Arrays》
- [Leetcode] 4 - Median of Two Sorted Arrays
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode #4 Median of Two Sorted Arrays
- LeetCode-4-Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- Leetcode[4] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- 关于Spring Data redis几种对象序列化的比较
- 自学Java系列 笔记2 面向对象2
- 解决服务器Apache Tomcat v7.0在本地无法在45秒内启动的问题。
- 第三十天 ViewPager
- scala学习之:隐式类与隐式对象
- leetcode 4: Median of Two Sorted Arrays
- Android fragment详解
- 语料库
- NSString常用的方法
- 为什么url需要对空格进行编码
- Java学习笔记----面向对象理解
- Python模块
- git常用命令--简洁版
- I2C SPI UART和CAN的区别