LeetCode题解:4. Median of Two Sorted Arrays
来源:互联网 发布:金手指炒股软件下载 编辑:程序博客网 时间:2024/06/06 02:29
中文翻译如下:
有两个排好序的数组,找出其中位数。
思考:
两个数组num1和num2的数字范围有以下几种情况:
(1) (2)
(3) (4)
(5) (6)
考虑到三个地方这道题基本就可以被拿下了:
首先自然要用到二分搜索。我的想法是这样的,每次迭代要比较两个地方,一个是两个数组中间数(体现在程序中是nums1[mid1]<=nums2[mid2]),另一个是两个中位数的索引和(half>mid1+mid2),这样产生了四种情况,可以剪切掉不同的部分。
其次考虑到可能出现其中一个数组开始和结束索引相同。
最后两个数组的长度和可能是偶数或者奇数都要考虑到。
最后把程序贴出来如下:
public double finddMedianSortedArrays(int[] nums1, int[] nums2) { final int total=nums1.length+nums2.length; final boolean even=(((total&1)==1)?false:true); final int half=total/2; int index1Start=0,index1End=nums1.length; int index2Start=0,index2End=nums2.length; while(true){ if(index1Start>=index1End){ index2Start=half-index1Start; break; } if(index2Start>=index2End){ index1Start=half-index2Start; break; } int mid1=(index1Start+index1End)/2; int mid2=(index2Start+index2End)/2; if(nums1[mid1]<=nums2[mid2]){ if(half>mid1+mid2){ index1Start=mid1+1; }else{ index2End=mid2; } }else{ if(half>mid1+mid2){ index2Start=mid2+1; }else{ index1End=mid1; } } } if(even){ double num1; if(index1Start<=0){ num1=nums2[index2Start-1]; }else if(index2Start<=0){ num1=nums1[index1Start-1]; }else{ num1=Math.max(nums1[index1Start-1], nums2[index2Start-1]); } double num2; if(index1Start>=nums1.length){ num2=nums2[index2Start]; }else if(index2Start>=nums2.length){ num2=nums1[index1Start]; }else{ num2=Math.min(nums1[index1Start], nums2[index2Start]); } return (num1+num2)/2; }else{ double num; if(index1Start>=nums1.length){ num=nums2[index2Start]; }else if(index2Start>=nums2.length){ num=nums1[index1Start]; }else{ num=Math.min(nums1[index1Start], nums2[index2Start]); } return num; } }
0 0
- LeetCode题解--4. Median of Two Sorted Arrays
- LeetCode题解:4. Median of Two Sorted Arrays
- Leetcode题解4.Median of Two Sorted Arrays
- LeetCode题解–4. Median of Two Sorted Arrays
- leetcode题解Java | 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays LeetCode题解
- leetcode题解-4. Median of Two Sorted Arrays
- LeetCode题解系列--4. Median of Two Sorted Arrays
- leetcode题解-4. Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode --- 4. Median of Two Sorted Arrays
- [Leetcode] 4. Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- ubuntu 12.04编译全志 SDK——FAQ
- JAVA笔记-String笔记(常量池)
- C语言中的static
- Python 元组 (教程 1)
- IOS常用代码总结 - 第三方库部分
- LeetCode题解:4. Median of Two Sorted Arrays
- ln 命令使用参数详解(ln -s 软链接)
- ffmpeg compile
- Microsoft HoloLens 技术解谜(上)
- 图片搜索网站大全
- leetcode: Word Pattern
- Cesium(三) 几何图形与外观
- 装饰者模式
- 工作思维定位,PDCA循环