public double FindMedianSortedArrays(int[] nums1, int[] nums2) {

int t=nums1.Length+nums2.Length;
int mid1=0;
int mid2=0;
int i=0;
int p1=0;
int p2=0;
while(p1<nums1.Length&&p2<nums2.Length&&i<=(t/2))
{
if(nums1[p1]<nums2[p2])
{
mid1=mid2;
mid2=nums1[p1];
p1++;
}
else
{
mid1=mid2;
mid2=nums2[p2];
p2++;
}
i++;
}
if(i<=(t/2))
{
while(p1<nums1.Length&&i<=(t/2))
{
mid1=mid2;
mid2=nums1[p1];
p1++;
i++;
}
while(p2<nums2.Length&&i<=(t/2))
{
mid1=mid2;
mid2=nums2[p2];
p2++;
i++; }
}
if(t%2==0)
return (mid1+mid2)/2.0;
else
return mid2; }

  合并两个有序数组,取中间的两个或者一个数

leetcode 4:Median of Two Sorted Arrays的更多相关文章

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

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

  3. 【LeetCode OJ】Median of Two Sorted Arrays

    题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...

  4. Leetcode Array 4 Median of Two Sorted Arrays

    做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...

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

  6. 【leetcode】4. Median of Two Sorted Arrays

    题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  7. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  8. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. 【LeetCode】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 ...

随机推荐

  1. Android为TV端助力 关于线程的那些事

    今天发现之前自己一直有个误区,new Runnable(run()方法){}原来它不是一定创建一个线程 如果用主线程的handler去post(Runnable),他就不会创建子线程,而是在主线程上执 ...

  2. Java面试题总结(不定期更新)

    1.HashMap和Hashtable的区别? HashMap:key.value都可以为空,线程不安全.初始容量16,扩容方式每次为2倍 Hashtable:不支持null key 和null va ...

  3. 闭包,jQuery插件的写法:图片预加载

    最近做的一些网页,单个网页图片量都比较大,网络不好的情况下,特别卡,这个图片预加载的方法可以牺牲一些时间换来网页的浏览顺畅,还是值得的. //闭包的写法,它内部的变量都是局部的,不会和外部巳有的变量进 ...

  4. MySQL 安装及卸载详细教程

    本文采用最新版MySQL8版本作为安装教程演示,本人亲试过程,准确无误.可供读者参考. 下载 官网下载 --> 社区免费服务版下载. 下载Windows安装程序MySQL Installer M ...

  5. Spark数据倾斜及解决方案

    一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...

  6. java开发基础知识学习

    java环境配置 classpath: .当前目录 path: java 命令所在目录 jdk安装目录 jdk/bin jre安装目录 jre/bin 安装JDK后配置环境变量如下: 安装过程用到了j ...

  7. SQL 事务隔离级别

    转载来源:https://www.cnblogs.com/chenmh/p/3998614.html 标签: SQL SEERVER/MSSQL SERVER/SQL/事务隔离级别选项/设置数据库事务 ...

  8. 在泛微系统中修改AD密码的配置

    参照文档: Windows server 2008 R2 安装AD域证书:https://blog.csdn.net/zhuyongru/article/details/81107839 配置泛微OA ...

  9. 阿里云ECS Ubuntu16.0 安装 uwsgi 失败解决方案

    Ubuntu安装包时报错 E:Unable to locate package xxx(如:python3-pip) 一般新安装Ubuntu后需要先更新软件源: apt-get update apt- ...

  10. python中自定义模块的引用

    在python中,实现能够在任何路径下都可以直接import,就必须将存放函数或类的文件添加到python的搜索路径. 其实很简单,主要步骤就是: import sys sys.path.append ...