给定两个递增数组arr1和arr2,已知两个数组的长度都为N,求两个数组中所有数的上中位数。
上中位数:假设递增序列长度为n,为第n/2个数
数据范围:,
要求:时间复杂度 ,空间复杂度
进阶:时间复杂度为,空间复杂度为
[1,2,3,4],[3,4,5,6]
3
总共有8个数,上中位数是第4小的数,所以返回3。
[0,1,2],[3,4,5]
2
总共有6个数,那么上中位数是第3小的数,所以返回2
[1],[2]
1
import java.util.*; public class Solution { public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { int num = (arr1.length+arr2.length)/2; int i=0,j=0,k=0,nums=0; while(i!=num){ nums = arr1[j]<arr2[k]?arr1[j++]:arr2[k++]; i++; } return nums; } }
import java.util.*; public class Solution { public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { int m = arr1.length; int n = arr2.length; int i = 0; int j = 0; int mid = -1; int index = 0; int count = (m + n) / 2; while (i < m && j < n) { if (arr1[i] <= arr2[j]) { mid = arr1[i++]; } else { mid = arr2[j++]; } index++; if (index == count) { return mid; } } while (i < m) { mid = arr1[i++]; index++; if (index == count) { return mid; } } while (j < n) { mid = arr2[j++]; index++; if (index == count) { return mid; } } return mid; } }
import java.util.Arrays; import java.util.stream.IntStream; public class Solution { public int findMedianinTwoSortedAray(int[] arr1, int[] arr2) { int[] arr3 = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray(); Arrays.sort(arr3); return arr3[(arr3.length -1) >> 1]; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here int result = 0; int length = arr1.length + arr2.length; int index = length / 2; int i = 0; int j = 0; for (;i + j < index - 1;) { if (arr1[i] < arr2[j]) { i++; } else { j++; } } result = arr1[i] < arr2[j] ? arr1[i] : arr2[j]; return result; } }
import java.util.*; // 暴力破解,空间O(1),时间O(n) public class Solution { public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { int n = arr1.length; int l = 0; int r = 0; int temp = 0; for (int i = 0; i < n; i++) { if (arr1[l] < arr2[r]) { temp = arr1[l]; l++; } else { temp = arr2[r]; r++; } } return temp; } }
// write code here //创建一个数组合并两个数组 List<Integer> list = new ArrayList<Integer>(); for(int i : arr1){ list.add(i); } for(int i : arr2){ list.add(i); } Collections.sort(list); //最终中位数肯定是第(arr1.length + arr2.length) / 2 个 return list.get((arr1.length + arr2.length) / 2 - 1); } 我这算不算投机取巧啊 感觉 你们写的都好牛逼
import java.util.*; public class Solution { /** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here int index1 = 0, index2 = 0; int count = 0; int min = 0; while(count < arr1.length){ if(arr1[index1] < arr2[index2]){ min = arr1[index1++]; count++; }else{ min = arr2[index2++]; count++; } } return min; } }
import java.util.*; public class Solution { /** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here //采用双指针 int target = (arr1.length + arr2.length) / 2; int left = 0, right = 0; int count = 0; while(left < arr1.length && right < arr2.length){ if(arr1[left] <= arr2[right]){ count++; if(count == target) return arr1[left]; left++; }else{ count++; if(count == target) return arr2[right]; right++; } } if(left == arr1.length){ while(count != target){ right++; count++; } return arr2[right]; } if(right == arr2.length){ while(count != target){ left++; count++; } return arr1[left]; } return -1; } }
import java.util.*; public class Solution { /** * find median in two sorted array * @param arr1 int整型一维数组 the array1 * @param arr2 int整型一维数组 the array2 * @return int整型 */ public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here int[] D=merge(arr1,arr1.length,arr2,arr2.length); //D的长度一定是偶数 题目给出 return D[D.length/2-1];//所以这里一定要减去1 可以使用一个测试用例来参考 } public int[] merge(int A[], int m, int B[], int n) { int[] help=new int[m+n]; int p1=0,p2=0,i=0; while(p1<m&&p2<n){ help[i++]=A[p1]<B[p2]?A[p1++]:B[p2++]; } while(p1<m){ help[i++]=A[p1++]; } while(p2<n){ help[i++]=B[p2++]; } return help; } }可以参考合并两个有序数组的代码,得到合并后的数组直接返回对应下标的数组值即可
public int findMedianinTwoSortedAray2 (int[] arr1, int[] arr2) { // write code here int len1 = arr1.length,len2 = arr2.length; int mid = (len1+len2)/2; int index1 = 0,index2=0; int count = 0; while(count++ < mid-1){ if(arr1[index1]<arr2[index2]){ index1++; }else{ index2++; } } return arr1[index1]<arr2[index2]?arr1[index1]:arr2[index2]; }
public int findMedianinTwoSortedAray (int[] arr1, int[] arr2){ int left1 = 0,left2 = 0; int right1 = arr1.length-1,right2 = arr2.length-1; int mid1=0,mid2=0; while(left1<right1){ mid1 = left1 + (right1-left1)/2; mid2 = left2 + (right2-left2)/2; int offset = (right1 - left1)%2; if(arr1[mid1]==arr2[mid2]){ return arr1[mid1]; }else if ((arr1[mid1]>arr2[mid2])){ right1 = mid1 ; left2 = mid2 + offset; }else{ left1 = mid1 + offset; right2 = mid2 ; } } return arr1[left1]<arr2[left2]?arr1[left1]:arr2[left2]; }
public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here int c = arr1.length; int i = 0; int j = 0; int index = 0; while(c>0) { if(arr1[i]<=arr2[j]) { index = arr1[i]; i++; c--; } else { index = arr2[j]; j++; c--; } } return index; }