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.排序法 时间复 ...
随机推荐
- SpringMVC源码解读 - HandlerMapping - SimpleUrlHandlerMapping初始化
摘要: SimpleUrlHandlerMapping只是参与Handler的注册,请求映射时由AbstractUrlHandlerMapping搞定. 初始化时,通过setMappings(Prop ...
- 第一课 了解SQL
1.1数据库基础 数据库:数据库是一个以某种有组织的方式存储的数据集合,可以想象是一个文件柜 数据库管理软件:DBMS用来操做创建数据库的软件 表:某种特定类型数据的结构化清单,数据库的下一层就是表 ...
- java外观模式
外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口 ...
- [国家集训队] tree Ⅱ
bzoj2631(权限题...):链接 落咕:链接 考察的是LCT维护链上信息. 但是这个题不一样的是又有加法又有乘法...(有木有想到落咕的模板--线段树2qwq) 因为乘法的运算优先度比加法高,所 ...
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study
死于update的一个long long写成int了 真的不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/ ...
- tcp 建立连接的三次握手,以及关闭连接的4次挥手
TCP连接的三次握手 第一次握手:客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认; (客户端问服务器:你爱我吗?) 第二次握手:服务器收到syn包,必须确认客户的 ...
- easyui里面的加载tree的两种方式
第一种: 使用EasyUI中Tree 符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的 [{ "id":1, "text":"My ...
- Levenshtein字符串距离算法介绍
Levenshtein字符串距离算法介绍 文/开发部 Dimmacro KMP完全匹配算法和 Levenshtein相似度匹配算法是模糊查找匹配字符串中最经典的算法,配合近期技术栏目关于算法的探讨,上 ...
- Kettle入门及性能优化FAQ
1.安装 配置Java环境 Java环境配置问题 java_home:D:\Program Files\Java\jdk1.7.0_25(安装jdk路径) classpath:.;%java_home ...