leetcode 4:Median of Two Sorted Arrays
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的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 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】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 ...
- 【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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
随机推荐
- 使用C#+PowerShell进行Windows系统间文件传输
新的winserver2016支持了一种nano模式,像以前的core模式,只能远程管理,只支持x64,只有610M,不让CentOS mini版独美. 这个nano版,默认只开启WinRM,所以只能 ...
- Kotlin 或将取代 Java——《Java 编程思想》作者 Bruce Eckel [转]
Bruce Eckel 是<Java 编程思想>.<C++编程思想>的作者,同时也是 MindView 公司的总裁,该公司向客户提供软件咨询和培训.他是 C++ 标准委员会拥有 ...
- ctypes库调用dll的个人见解
最近着手开发一个小东西涉及到了API接口的知识点, 第一次使用到了ctypes库,在网上找了一大圈,基本都是讲add.dll之后就没了. 就像下面这个: from ctypes import * dl ...
- 结对编程--四则运算(Java)萧英杰 夏浚杰
结对编程--四则运算(Java)萧英杰 夏浚杰 Github项目地址 功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 使用 -n 参数控制生成题目的个数(实现) 使用 -r 参数控制题目 ...
- [20190227]简单探究tab$的bojb#字段.txt
[20190227]简单探究tab$的bojb#字段.txt --//上午做了删除tab$表,其对应索引i_tab1的恢复,我一直以为这个索引会很大,没有想到在我的测试环境仅仅139个键值.--//查 ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- CentOS7中安装MySQL5.7
安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...
- 【RHEL7.0】软件包管理
1.常用的RPM软件包命令 安装软件的命令格式 rpm –ivh filename.rpm 升级软件的命令格式 rpm –Uvh filename.rpm 卸载软件的命令格式 rpm –e fi ...
- CentOS7安装redis数据库及php-redis扩展
redis 首先把redis安装到服务器中 1.wget http://download.redis.io/redis-stable.tar.gz 下载redis源码 2. tar xvzf redi ...
- Java的基础知识三
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...