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. 1.Odoo产品分析系列 – 目录

    Odoo产品分析 (一) – 一切为零 Odoo产品分析 (二) – 商业板块(1) – 销售(1) Odoo产品分析 (二) – 商业板块(1) – 销售(2) Odoo产品分析 (二) – 商业板 ...

  2. PMS 修改禅道默认首页元素及展示

    修改禅道默认首页元素及展示 by:授客 QQ:1033553122 测试环境: 禅道项目管理软件ZenTaoPMS.9.5.1.win64 需求描述 如下,安装禅道后访问默认首页,展示如下,我们希望它 ...

  3. Android Studio 无法预览xml布局视图:failed to load AppCompat ActionBar with unkNown error

    问题如下: 解决方法: 找到res-->values-->styles.xml 文件 可以看到主题Them设置如下: 修改为: 界面预览可以正常显示

  4. Android 9.0新特性

    1.全面屏支持,Android P加入了对刘海屏的支持,谷歌称之为凹口屏幕(display with a cutout).借助最新的提供的DisplayCutout类,开发者可以找到非功能区域的位置和 ...

  5. day15(PYTHON)推导式{生成器,字典,列表,集合}

    #[每一个元素或者是和元素相关的操作 for 元素 in 可迭代数据类型] #遍历之后挨个处理 #[满足条件的元素相关的操作 for 元素 in 可迭代数据类型 if 元素相关的条件] #筛选功能 # ...

  6. 原型链、闭包四种作用、继承、命名空间、枚举类型(day13)

    原型链 JavaScript 对象是动态的属性“包”(指其自己的属性).JavaScript 对象有一个指向一个原型对象的链.当试图访问一个对象的属性时,它不仅仅在该对象上搜寻,还会搜寻该对象的原型, ...

  7. (网页)websocket例子

    转载自博客园张果package action; import javax.websocket.CloseReason; import javax.websocket.OnClose; import j ...

  8. 实现wc部分功能 java

    GitHub地址:https://github.com/carlylewen/ruangong 相关要求 基本功能 wc.exe -c file.c     //返回文件 file.c 的字符数(实现 ...

  9. Springboot helloworld入门最经典例子

    一.建立maven java项目 导入springboot包 二.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  10. [20180918]文件格式与sql_id.txt

    [20180918]文件格式与sql_id.txt --//记录测试中遇到的一个问题.这是我在探究SQL*Net more data from client遇到的问题.--//就是实际oracle会把 ...