2024/9/15 LeetCode Hot100 二分查找最后一题
This commit is contained in:
parent
5b21fc35af
commit
ad3e07dc61
|
@ -0,0 +1,40 @@
|
|||
package com.cool.hot100.binary_search;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/15/23:15
|
||||
* DayNumber 2
|
||||
* Hard 3
|
||||
* Level 8
|
||||
*/
|
||||
public class Num4 {
|
||||
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
|
||||
int len1 = nums1.length;
|
||||
int len2 = nums2.length;
|
||||
int left=-1;
|
||||
int right=-1;
|
||||
int index1 = 0;
|
||||
int index2 = 0;
|
||||
for(int i=0;i<=(len1+len2)/2;i++){
|
||||
left=right;
|
||||
if(index1<len1&&(index2>=len2||nums1[index1]<nums2[index2])){
|
||||
right=nums1[index1++];
|
||||
}else{
|
||||
right=nums2[index2++];
|
||||
}
|
||||
}
|
||||
if(((len1+len2)&1)==0){
|
||||
return (left+right)/2.0;
|
||||
}else{
|
||||
return right;
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test(){
|
||||
findMedianSortedArrays(new int[]{1,2},new int[]{3,4});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue