4:Median of Two Sorted Arrays
here 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)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
答案:
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length,len2 = nums2.length;
int len = len1 + len2;
int mod = len%2;
int start = 0;
int middle = len/2;
int temp = 0;
int index1 = 0,index2 = 0;
while(index1<len1 || index2 < len2){
if(index2 == len2 || (index1 < len1 && nums1[index1]<=nums2[index2])){
if(mod==1){
if(start == middle){
return nums1[index1];
}
}else{
if(start == middle - 1){
temp = nums1[index1];
}
if(start == middle){
return ((double)temp + (double)nums1[index1])/2;
}
}
index1++;
}else if(index1 == len1 || (index2 < len2 && nums1[index1]>nums2[index2])){
if(mod==1){
if(start == middle){
return nums2[index2];
}
}else{
if(start == middle - 1){
temp = nums2[index2];
}
if(start == middle){
return ((double)temp + (double)nums2[index2])/2;
}
}
index2++;
}
start ++;
}
return 0.0;
}
4:Median of Two Sorted Arrays的更多相关文章
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- LeetCode2: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 sor ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- Q4:Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- leetcode 4:Median of Two Sorted Arrays
public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- yii2项目实战-路由美化以及如何正确的生成链接
yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...
- Java书籍经典Top10
1)Java Language Specification, Third Edition (by James Gosling) 本书由Java技术的发明者编写,是Java TM编程语言的权威性技术指南 ...
- Java泛型(泛型接口、泛型类、泛型方法)
转载 转载出处:https://www.cnblogs.com/JokerShi/p/8117556.html 泛型接口: 定义一个泛型接口: 通过类去实现这个泛型接口的时候指定泛型T的具体类型. 指 ...
- 再次学习linux文件特殊权限:SUID、SGID、Sticy Bit
以前对于文件管理的认识只限于UGO的管理,对于特殊权限的学习还是一知半解.重新学习了一遍,我自己理解的东东记录一下. 首先,列一下SUID.SGID.Sticy Bit所代表的权限数值.就好像rwx分 ...
- Python之迭代器,生成器与装饰器
1>迭代器原理及使用: 1>原理: 迭代器是访问集合元素的一种方式,迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束:迭代器只能往前不会后退,不过这也没什 ...
- Vue.js的库,包,资源的列表大全。
官方资源 外部资源 社区 播客 官方示例 入门 开发工具 语法高亮 代码片段 自动补全 组件集合 库和插件 路由 ajax/数据 状态管理 校验 UI组件 i18n 示例 模板 脚手架 整合 插件/指 ...
- [转]一次Delete&Insert引发的Mysql死锁
近日遇到一个比较奇怪的deadlock错误, 错误详情: Deadlock found when trying to get lock; try restarting transaction; nes ...
- 2014-3tomcat遇到的问题汇总
tomcat启动不起来 端口占用:加了apache的ajp,所以要打开ajp_port,结果被占用了. 权限不够:chown:各个文件的权限都不够,特别是日志文件的. 配置问题:应用 servlet- ...
- error: field 'b' has imcomplete type
在下面的程序中,在编译时会遇到下面的错误: error: field 'b' has incomplete type 域b是一个不完备的类型,即class B的声明不完备 #include <i ...
- Code First ef SQL Server 版本不支持数据类型“datetime2”
When calling DbContext.SaveChanges, I get a DbUpdateException:An unhandled exception of type 'System ...