leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays
这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数。
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int numsSize = nums1Size + nums2Size;
int *nums = (int*)malloc(sizeof(int)*numsSize);
double median;
//合并
for (int i = , j = , k = ; k < numsSize; k++) {
if (i >= nums1Size) nums[k] = nums2[j++];
else if (j >= nums2Size) nums[k] = nums1[i++];
else {
if (nums1[i] >= nums2[j])nums[k] = nums2[j++];
else nums[k] = nums1[i++];
}
}
//中卫
if (numsSize % == )
median = (double) (nums[(int)(numsSize / - )] + nums[(int)(numsSize / )]) / ;
else median = (double)nums[numsSize / ];
free(nums);
return median;
}
。
leetcode.C.4. Median of Two Sorted Arrays的更多相关文章
- 【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第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- 【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.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【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 OJ 4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- 运维堡垒机----Gateone
简介: 运维堡垒机的理念起源于跳板机.2000年左右,高端行业用户为了对运维人员的远程登录进行集中管理,会在机房里部署跳板机.跳板机就是一台服务器,维护人员在维护过程中,首先要统一登录到这台服务器上, ...
- 【转】Base64算法详解
原文链接:https://blog.csdn.net/robertcpp/article/details/51628647 完整的BASE64定义可见RFC 1421和RFC 2045.编码后的数据比 ...
- Linux服务器开启tomcat的gc日志
压力测试,为了能监控长期对gc的变化的情况,那么就需要在tomcat中进行配置相关的gc输入日志,以便后续来对gc中进行分析 工具 :linux+tomcat 1.进入到了tomcat的bin的目录下 ...
- 第120天:移动端-Bootstrap基本使用方法
一.Bootstrap使用 1.搭建Bootstrap页面骨架及项目目录结构 ``` ├─ /weijinsuo/ ··················· 项目所在目录 └─┬─ /css/ ···· ...
- 整合SSM框架应用
普通方式 新建spring模块时引入如下内容: 启用devtools插件(热部署插件) idea需要做如下配置 settings-build-compiler->勾选build project ...
- hdu 6375 百度之星 度度熊学队列
题目链接 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 ...
- MySQL复制 -- 应用场景
本文行文路径如下: 什么是复制?复制是怎么工作的?复制有哪几种表现形式?复制能解决那些问题?业界有哪些数据同步解决方案? 什么是复制? 官方解释道:Replication enables data f ...
- BZOJ3598 SCOI2014方伯伯的商场之旅(数位dp)
看到数据范围就可以猜到数位dp了.显然对于一个数最后移到的位置应该是其中位数.于是考虑枚举移到的位置,那么设其左边和为l,左右边和为r,该位置数为p,则需要满足l+p>=r且r+p>=l. ...
- P4622 [COCI2012-2013#6] JEDAN
题目背景 COCI 题目描述 有N个数排成一行(数值代表高度),最初所有的数都为零,你可以选择连续的一段等高的数,将它们都增加1(除了开头和结尾那个数)如下图表示了两次操作: 现在有一些数字看不清了, ...
- Docker学习笔记二:Docker常用命令及提升拉取镜像的速度
一.Docker命令: 1.docker images //命令用来查看docker中所包含的镜像信息 2.docker ps -a //命令用来查看docker中所包含所有容器信息(运行状 ...