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. C# 对象持久化

    本文以一个简单的小例子,简述对象持久化的相关知识,仅供学习分享使用,如有不足之处,还请指正. 概述 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术. 本文介绍的是除数据库 ...

  2. wap2app(二)-- 设置APP系统状态栏

    准备 工具:HBuilder 一.全屏设置,不显示系统状态栏 这里所说的系统状态栏就是包括了:信号.运营商.电量等信息手机屏幕最顶部信息. 全屏并非状态栏透明或变色,而是没有状态栏,也就是看不见电量. ...

  3. 你不可不知的Java引用类型【总结篇】

    四种引用类型总结 引用级别:强引用 > 软引用 > 弱引用 > 虚引用 理解 就如最开始说的,设置四种引用类型,是为了更好的控制对象的生命周期,让代码能够一定程度上干涉GC过程,所以 ...

  4. Linux 最小化安装后IP的配置(DHCP获取IP地址)

    图形化Linux的DHCP好配置,我就不讲了.主要将一下Linux最小化安装后IP的配置: linux最小化安装后没有ifconfig这个命令: yum install net-tools.x86_6 ...

  5. C#-简介(一)

    1.C#语言简介 C#计算机语言 是一门高级计算机语言 他的开发模式更接近人类和社会的思维模式,有助于提高开发效率 C#历史 1998年COOL这个项目是C#语言的前身,由微软 Anders Hejl ...

  6. web前端(6)—— 标签的属性,分类,嵌套

    属性 HTML标签可以设置属性,属性一般以键值对的方式写在开始标签中 1.HTML标签除一些特定属性外可以设置自定义属性,一个标签可以设置多个属性用空格分隔,多个属性不区分先后顺序. 2.属性值要用引 ...

  7. C# 中将月份格式化为英语缩写格式

    在测试Android 系统的时候,日期输入框需要输入英语短格式,如下. 考虑到系统日期格式和地域的关系紧密,地域不同,日期格式不同,所以经过查找,找到下面的解决方法. date.ToString(&q ...

  8. shell 获取时间

    获取当前时间 t=$(date +"%Y-%m-%d %H-%M-%S") echo $t 获取前一天的当前时间 time=$(date -d "-1 day" ...

  9. C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法

    使用反射(Reflect)获取dll文件中的类型并调用方法 需引用:System.Reflection; 1. 使用反射(Reflect)获取dll文件中的类型并调用方法(入门案例) static v ...

  10. Servlet(一):Hello Servlet

    今天介绍的专题是servlet和jsp,属于web开发中的基础,先来实际操作一下servlet,创建你的第一个web小项目. 1.新建一个maven项目. 鼠标右键单击eclipse左边项目栏的空白区 ...