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.排序法 时间复 ...
随机推荐
- [GO]接口的嵌用继承
package main import "fmt" type Humaner interface { SayHi() } type Personer interface { Hum ...
- [GO]匿名字段的同名字段操作
package main import ( "fmt" ) type Person struct { name string sex byte age int } type Stu ...
- javascript总结25:Array的5中迭代方法: every(), filter() , forEach() map() some()
1 Array常用的操作方法: 1 操作方法 - concat() //把参数拼接到当前数组 -slice() //从当前数组中截取一个新的数组,不影响原来的数组,参数start从0开始,end从1开 ...
- TCP协议三次握手连接四次握手断开和DOS攻击
转载:http://blog.csdn.net/fw0124/article/details/7452695 TCP连接的状态图 TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 贴一个tel ...
- 8) Struts2 2 SpringMVC
git@github.com:witaste/smse.git 数据库脚本: /* Navicat MySQL Data Transfer Source Server : 新服务器 Source Se ...
- 编写高质量代码改善C#程序的157个建议——建议157:从写第一个界面开始,就进行自动化测试
建议157:从写第一个界面开始,就进行自动化测试 如果说单元测试是白盒测试,那么自动化测试就是黑盒测试.黑盒测试要求捕捉界面上的控件句柄,并对其进行编码,以达到模拟人工操作的目的.具体的自动化测试请学 ...
- 深入探讨 Java 类加载器(转载)
类加载器(class loader)是 Java™中的一个很重要的概念.类加载器负责加载 Java 类的字节代码到 Java 虚拟机中.本文首先详细介绍了 Java 类加载器的基本概念,包括代理模式. ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- EasyFastCMS系列教学课程——1、三层框架的搭建
在本系列教程中,我们以一个大型CMS系统的完整开发流程为例,和大家一起探讨net开发的经验和教训.在本程序中,我们采用了流行的三层/N层框架+仓储模式的架构模式.项目分层示意图: 各层的主要用 ...
- Oracle 字符集常见字符集及解决方案
Oracle 字符集常见字符集及解决方案 优先级别:alter session>环境变量>注册表>参数文件 一.查看字符集: 1.查询服务端字符集: select userenv(' ...