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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Q4:Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. leetcode 4:Median of Two Sorted Arrays

    public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...

  9. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. 洛谷P2634 [国家集训队]聪聪可可 (点分治)

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...

  2. Android-ContentProvider原理图

    ContentProvider的设计思想是模仿了Web里面的架构思想: Web服务器   对外暴露数据(提供被访问的地址Uri,并允许给客户端访问,也可以只让客户端访问某些行为) ContentPro ...

  3. PMBOK项目管理认知概要

    2015年6月,通过努力取得PMP证书,很是欣喜,也是对努力付出的一种奖励吧! 通过学习PMP相关的项目管理的知识,对国外的项目管理技术有更加系统的认知.理解.掌握,熟悉全项目生命周期的管理. 其实对 ...

  4. php获取数组的键值

    <?php header ( "Content-type: text/html; charset=utf-8" ); $info[; $info[; $info[; $inf ...

  5. js常用的校验代码 (整理)

    /* 用途:检查输入手机号码是否正确 输入:str:字符串 返回:如果通过验证返回true,否则返回false */ function checkMobile(str){ var regu =/^[1 ...

  6. 小程序:web-view采坑指南

    最近负责开发的[广州医保查询]小程序已经发布上线,其中使用web-view组件完成的[在线绑定社保卡]核心流程,遇到了一些坑,现总结如下: 首先,让我们一起看看什么是web-view ? 小程序api ...

  7. 用户画像,知乎Live总结

    ttps://www.zhihu.com/lives/889189116527403008/messages 用户画像两层含义:单个标签:用户的分布 标签体系要与时俱进,如果标签被下游强依赖,则不轻易 ...

  8. python 小点

    python中列表不能除以列表,列表不能除以整数.浮点数. numpy数组可以实现数组除以整数.

  9. better-scroll在vue中的应用

    在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了,以滴滴为例,可以是这样竖向滚动的列表,如图所示: 微信 —> 钱包—>滴滴出行”体验效果. 什么是 better-scrol ...

  10. 洛谷P4254 [JSOI2008]Blue Mary开公司(李超线段树)

    题面 传送门 题解 李超线段树板子 具体可以看这里 //minamoto #include<bits/stdc++.h> #define R register #define fp(i,a ...