当前位置:网站首页>4. Find the median of two positive arrays
4. Find the median of two positive arrays
2022-07-23 14:32:00 【ATTACH_ Fine】
subject
Given two sizes, they are m and n Positive order of ( From small to large ) Array nums1 and nums2. Please find and return the values of these two positive ordered arrays Median .
The time complexity of the algorithm should be O(log (m+n)) .
Example :
Ideas
According to the definition of the median , When m+n It's an odd number , The median is the... Of two ordered arrays (m+n)/2 Elements , When m+n When it's even , The median is the... Of two ordered arrays (m+n)/2 Elements and number (m+n)/2+1 The average of the elements . therefore , You can find the second question in the order k Small number , among k by (m+n)/2 or (m+n)/2+1.

Code
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
int totalLen = len1 + len2;
// Odd number , The median is the... Of two ordered arrays (m+n)/2 Elements
if(totalLen % 2 == 1){
double median = getKthElement(nums1,nums2,totalLen/2+1);
return median;
}else{
// Even number , The median is the... Of two ordered arrays (m+n)/2 Elements and number (m+n)/2+1 The average of the elements
double median = (getKthElement(nums1,nums2,totalLen/2) + getKthElement(nums1,nums2,totalLen/2 + 1)) / 2.0;
return median;
}
}
public int getKthElement(int[] nums1,int[] nums2,int k){
int len1 = nums1.length;
int len2 = nums2.length;
int index1 = 0, index2 = 0;
while(true){
if(index1 == len1){
return nums2[index2+k-1];
}
if(index2 == len2){
return nums1[index1+k-1];
}
// We're looking for number one 1 Small numbers , So you just need to judge which number is smaller in the first two arrays
if(k==1){
return Math.min(nums1[index1],nums2[index2]);
}
int half = k / 2;
// Delete " Some elements were added ( These elements are better than k Small elements should be small ) to update index Value
// If it exceeds the length of the array , Just point to the end of the array
int newIndex1 = Math.min(index1 + half,len1) - 1;
int newIndex2 = Math.min(index2 + half,len2) - 1;
if(nums1[newIndex1] <= nums2[newIndex2]){
k -= (newIndex1-index1+1);
index1 = newIndex1 + 1;
}else{
k -= (newIndex2-index2+1);
index2 = newIndex2 + 1;
}
}
}
}
边栏推荐
- spotlight灯箱js插件全屏放大图片
- 手机股票开户风险性大吗,安全吗?
- Summary of different circulation modes and precautions in JS
- 基于EFR32MG24的AI 加速度姿势识别体验
- ValidationError: Invalid options object. Dev Server has been initialized using an options object th
- Drag and drop----
- Pycharm读取Excel文件时报错:raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+ ; not supported )
- JS to implement encode64 encryption
- pageHepler丢失原sql order by条件的坑
- JS calendar style pie chart statistics plug-in
猜你喜欢

ValidationError: Invalid options object. Dev Server has been initialized using an options object th
![webstrom ERROR in [eslint] ESLint is not a constructor](/img/e9/b084512d6aa8c4116d7068fdc8fc05.png)
webstrom ERROR in [eslint] ESLint is not a constructor

Authing 支持 Zadig 啦!云原生用户统一认证快速对接

Aruba学习笔记05-配置架构- WLAN配置架构

剑指 Offer 46. 把数字翻译成字符串

完全背包!
![webstrom ERROR in [eslint] ESLint is not a constructor](/img/e9/b084512d6aa8c4116d7068fdc8fc05.png)
webstrom ERROR in [eslint] ESLint is not a constructor

STM32 output sine wave +cubemx configuration +hal Library

几种点云(网格)孔洞填充方法(1)

关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
随机推荐
Pagehepler lost the pit of the original SQL order by condition
JS to implement encode64 encryption
Offline data interoperability
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
pageHepler丢失原sql order by条件的坑
Stream stream is used for classification display.
Tell you Web3.0 I understand
利用js自动解析执行xss
Optimize Huawei ECs to use key login
Process blocks and methods
【PyQt5安装以及使用】
Shell practice: one click start process, close process and view process status
JS texture style pie chart plug-in
js纹理样式饼图插件
第三章 复杂一点的查询
Summary of JS data type judgment methods
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)
(重链剖分)魔法树
Towhee 每周模型
关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论