LeetCode-4 :寻找两个有序数组的中位数 ★★★★★
2019-03-27 11:03
447 查看
版权声明:wx: Symirror https://blog.csdn.net/qq_41960416/article/details/88839268
leetcode 解决方案 leetcode 源代码(java)
寻找两个有序数组的中位数
问题描述
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
My 解决方案描述
1,统计两个数组中不重复元素的个数,并以此创建新数组存放结果
2,输出中位数(判断数组奇偶个数)
My 源代码(java)
// 获取中位数的方法 static class Solution { public static double findMedianSortedArrays(int[] nums1, int[] nums2) { // 获取无重复数组的长度len int len = 0 ;//存放当前长度 int max = -1;//存放当前最大值 int i = 0 ;//遍历索引 int j = 0 ; while(i < nums1.length || j < nums2.length){ //分别判断判断是否为已统计元素========================================== if(max != -1){ if(i < nums1.length && nums1[i] == max){ //nums1重复 i++; continue; } if(j < nums2.length && nums2[j] == max){ //nums2重复 j++; continue; } } //如果当前两元素均未添加过,继续添加========================================= if(i < nums1.length && j < nums2.length){ //判断两数组当前元素是否相同,统计较小元素 if(nums1[i] > nums2[j]){ //nums1大 max = nums2[j]; len++; j++; }else if(nums1[i] < nums2[j]){ //nums2大 max = nums1[i]; len++; i++; }else{ //相等 max = nums1[i]; len++; i++; j++; } }else if(i == nums1.length){ max = nums2[j]; len++; j++; }else{ max = nums1[i]; len++; i++; } } //测试长度 System.out.println("测试新数组长度:"+len); //创建长度为len的数组arr存放结果============================================== int[] arr = new int[len]; //遍历两个数组提取元素到结果数组arr max = -1 ; //归零max i = 0; j = 0; //归零遍历索引 int index = 0; //arr的索引 while(i < nums1.length || j < nums2.length){ //分别判断判断是否为已统计元素 if(max != -1){ if(i < nums1.length && nums1[i] == max){ //nums1重复 i++; continue; } if(j < nums2.length && nums2[j] == max){ //nums2重复 j++; continue; } } if(i < nums1.length && j < nums2.length){ //判断两数组当前元素是否相同,统计较小元素 if(nums1[i] > nums2[j]){ //nums1大 max = nums2[j]; arr[index] = max; index++; j++; }else if(nums1[i] < nums2[j]){ //nums2大 max = nums1[i]; arr[index] = max; index++; i++; }else{ //相等 max = nums1[i]; arr[index] = max; index++; i++; j++; } }else if(i == nums1.length){ max = nums2[j]; arr[index] = max; index++; j++; }else{ max = nums1[i]; arr[index] = max; index++; i++; } } //测试数组 for (int k : arr) { System.out.println("测试新数组:"+k); } //获取中位数 double mid; if(len % 2 == 0){ mid = arr[len/2-1] + arr[len/2] ; mid = mid/2; }else{ mid = arr[len/2]; } //返回结果中位数 return mid; } }
leetcode 解决方案
leetcode 源代码(java)
class Solution { public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = B.length; if (m > n) { // to ensure m<=n int[] temp = A; A = B; B = temp; int tmp = m; m = n; n = tmp; } int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2; while (iMin <= iMax) { int i = (iMin + iMax) / 2; int j = halfLen - i; if (i < iMax && B[j-1] > A[i]){ iMin = i + 1; // i is too small } else if (i > iMin && A[i-1] > B[j]) { iMax = i - 1; // i is too big } else { // i is perfect int maxLeft = 0; if (i == 0) { maxLeft = B[j-1]; } else if (j == 0) { maxLeft = A[i-1]; } else { maxLeft = Math.max(A[i-1], B[j-1]); } if ( (m + n) % 2 == 1 ) { return maxLeft; } int minRight = 0; if (i == m) { minRight = B[j]; } else if (j == n) { minRight = A[i]; } else { minRight = Math.min(B[j], A[i]); } return (maxLeft + minRight) / 2.0; } } return 0.0; } }
相关文章推荐
- LeetCode第4题--寻找两个有序数组的中位数
- 寻找两个有序数组的中位数(LeetCode)
- LeetCode4.python实现:寻找两个有序数组中的中位数问题☆☆☆
- LeetCode--4. 寻找两个有序数组的中位数(Median of Two Sorted Arrays)
- LeetCode刷题-寻找两个有序数组的中位数
- leetcode之数组类之数组的旋转与分治类-----OJ 189/33/81/153/154 数组旋转 旋转数组搜索 88 有序数组合并 4 两个有序数组寻找第K个元素/中位数 35 寻找插入位置
- leetcode 寻找两个有序数组的中位数
- [LeetCode]4. 寻找两个有序数组的中位数/C++
- Leetcode-寻找两个有序数组的中位数
- 基础算法之寻找两个有序数组的中位数(LeetCode-4)
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- leetcode 4. 寻找两个有序数组的中位数(Java版)
- 【LeetCode】第四题:寻找两个有序数组的中位数
- Leetcode(C++)——4. 寻找两个有序数组的中位数
- LeetCode题解(python)-4. 寻找两个有序数组的中位数
- (1.2.4.2)寻找两个有序数组中的第K个数或者中位数
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- 力扣寻找两个有序数组的中位数
- 【Leetcode】两个有序数组的中位数
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数