leetcode 之Median of Two Sorted Arrays(五)
找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值。
参考下面的思路,非常的清晰:
代码:
double findMedianofTwoSortArrays(int A[], int B[], int m, int n){int total = m + n;//判断序列长度的奇偶,奇数只有一个中间值,偶数有两个中间值,取平均if (total & 0x1)return find_kth(A, m, B, n, total / 2 + 1);elsereturn (find_kth(A, m, B, n, total / 2) + find_kth(A, m, B, n, total / 2 + 1)) / 2;}int find_kth(int A[], int m, int B[], int n, int k){//设定m<=nif (m > n)return find_kth(B, n, A, m, k);if (m == 0)return B[k - 1];if (k == 1)return min(A[0], B[0]);//删除掉一部分数据int ia = min(k / 2, m), ib = k - ia;if (A[ia - 1] < B[ib - 1])return find_kth(A + ia, m - ia, B, n, k - ia);else if (A[ia - 1]>B[ib - 1])return find_kth(A, m, B + ib, m - ib, k - ib);elsereturn A[ia - 1];}
View Code
转载于:https://www.cnblogs.com/573177885qq/p/5492407.html
leetcode 之Median of Two Sorted Arrays(五)相关推荐
- 【leetcode】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 s ...
- 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 the two ...
- 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 the ...
- 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 two ...
- leetcode - 4. Median of Two Sorted Arrays
Degree Easy ★ (*´╰╯`๓)♬ Description: There are two sorted arrays nums1 and nums2 of size m and n res ...
- 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 the ...
- java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...
题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
最新文章
- python怎么使用int四舍五入_python中如何取整数
- 安卓从sd引导linux,从android中的adb shell手动挂载SD卡
- java jni.h_java-如何使jni.h被找到?
- asp.net甘特图控件exG2antt介绍及免费下载
- 史上最成功的数学预测:用狄拉克方程推导出电子自旋
- DevOps(过程、方法与系统的统称)是什么
- Spark基础学习笔记01:初步了解Spark
- width:100%以什么为基准的测试
- 通达信指标公式颜色代码、图标、函数 大全
- 小程序跳转样式布局错乱_微信小程序页面布局问题
- python3.0编程软件_震惊!国内已知第一款Scratch 3.0编程软件KittenBlock推出Python编程功能!...
- Cocos Creator下JavaScript模拟砸金蛋3d旋转效果,附代码
- 链路聚合Eth-Trunk
- sublime 3 自定义配置
- 警务系统三维可视化管理与情报研判综合分析平台
- 5星|戴蒙德《为什么有的国家富裕,有的国家贫穷》:为什么有的国家能发展出好制度...
- 比较好的网页视频播放器总结
- Leetcode 45. 跳跃游戏 II
- A tutorial on Principal Components Analysis - 主成分分析(PCA)教程
- 本机微信开发环境搭建
热门文章
- Docker将镜像上传至私有仓库
- Python Django 表单类Form(py代码画form表单仅渲染页面)
- Scala高阶函数详解
- centos7自带python版本_CentOS7保留默认Python版本并安装更新Python2和Python3共存
- 【已解决】Win7搭建Python环境:Eclipse + PyDev插件
- Matlab---傅里叶变换---通俗理解(二)
- python中cd是什么意思_python的cd的
- 1.4 计算机系统概述思维导图小结-常见问题和易混淆知识点(组成原理)
- python 手动读取cifar10_Python搞定Excel,秒解决!大大提高工作效率
- 需求获取安排计划书_创业型公司商业计划书PPT怎么写?才是投资人最想看的,免费领取...