当前位置:网站首页>Force buckle - 4. Find the median of two positive arrays
Force buckle - 4. Find the median of two positive arrays
2022-07-26 06:28:00 【weixin_ fifty-four million ninety-six thousand two hundred and 】
subject :

Ideas :

1. First merge the two arrays , Calculate the array separately 1 And an array 2 The length of l=m+n;
2. Define two pointers , Put them in the last position of the two arrays ;
3. Judge , Because it's positive order ( From small to large ) Array of , So we can judge nums1 Whether the first number is greater than nums2 First number nums1[aStart]<nums2[bStart]
4. Judge , Is there any remaining number , If there is , Put it right in the back
Code :
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
// Composite array
int[] res = new int[len1+len2];
int index = 0;
int left = 0, right = 0;
// Merge and sort the composite part
while(left<len1&&right<len2) {
if(nums1[left]<nums2[right]) {
res[index++] = nums1[left++];
} else {
res[index++] = nums2[right++];
}
}
// Judge nums1 Remaining or nums2 The remaining , And add the rest to the composite array
while(left<len1) {
res[index++] = nums1[left++];
}
while(right<len2) {
res[index++] = nums2[right++];
}
// Judge the length of the synthesized array , Find the median . The return is double type
if((len1+len2)%2==1) {
return (double) res[(len1+len2)/2];
} else {
return (double) (res[(len1+len2)/2]+res[(len1+len2)/2-1])/2;
}
}
}
Upgrade code :
public double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
int len = m + n;
// left Used to save the last traversal result ,right The table looks at the result of the current traversal . Because even numbers : Two values are required
int left = -1, right = -1;
// aStart Point to the moment A The position of the array ,bStart Point to the moment B The position of the array
int aStart = 0, bStart = 0;
for (int i = 0; i <= len / 2; i++) {
// Every time you start a new side traversal , Save the last traversal result
left = right;
// Array A The pointer aStart The condition of moving back :aStart Not to A Last , here A The number of positions is less than B The number of positions . At this point, judge B The pointer to bStart Is it across the border? , If you cross the boundary, don't judge at this time A The value of the position is the same as B The size of the value of the position , Instead, continue to traverse A The value of the current position .
if (aStart < m && (bStart >= n || A[aStart] < B[bStart])) {
right = A[aStart++];
} else {
right = B[bStart++];
}
}
if ((len & 1) == 0)
return (left + right) / 2.0;
else
return right;
}
Reference link :
边栏推荐
- Map dictionary and constraints of go
- 【Day_05 0422】统计回文
- Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
- 【无标题】
- Distributed | practice: smoothly migrate business from MYCAT to dble
- C语言进阶——可存档通讯录(文件)
- 【Day02_0419】C语言选择题
- Upgrade appium automation framework to the latest 2.0
- Cdga | how to build data asset catalogue?
- 【Day_07 0425】合法括号序列判断
猜你喜欢

Viewing the technology stack of distributed system from the crash report of station B

Design principle of infrared circuit of single chip microcomputer

【pytorch】微调技术

JVM class loading and GC garbage collection mechanism

redis 哨兵集群搭建

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

Do it yourself smart home: intelligent air conditioning control

Embedded sharing collection 14

YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
C language explanation series - comprehensive exercises, guessing numbers games
随机推荐
nuxt 配置主题切换
Go的map字典及约束
英语句式参考纯享版 - 状语从句
[day04_0421] C language multiple choice questions
【Day_02 0419】倒置字符串
BigDecimal变为负数
RNN recurrent neural network
[untitled]
WebAPI整理
English sentence pattern reference exclusive Edition - adverbial clause
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
【Day_07 0425】Fibonacci数列
BigDecimal becomes negative
【Day05_0422】C语言选择题
How can machinery manufacturing enterprises do well in production management with the help of ERP system?
【无标题】
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
【Day_03 0420】数组中出现次数超过一半的数字
Slice and array of go
Viewing the technology stack of distributed system from the crash report of station B