leetcode-4Median of Two Sorted Arrays
来源:互联网 发布:结婚相片制作软件 编辑:程序博客网 时间:2024/06/05 20:31
寻找2个有序表的中位数,直接递归搞。
import java.util.* ;public class Solution{ public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n = nums1.length ; int m = nums2.length ; int d = n + m ; if((d & 1) == 1) return 1.0 * findKth(nums1, n, nums2, m, d/2 + 1) ; else return 0.5 * (findKth(nums1, n, nums2 , m, d/2) + findKth(nums1, n, nums2, m, d/2+1) ) ; } public int findKth(int[] a, int n , int[] b , int m , int k){ if(n > m) // ensure n <= m return findKth(b, m, a, n, k) ; if(n == 0) return b[k-1] ; if(k == 1) return Math.min(a[0], b[0]) ; int pa = Math.min(k/2 , n) ; int pb = k - pa ; if(a[pa-1] < b[pb-1]) return findKth(Arrays.copyOfRange(a, pa , n) , n-pa , b, m, k-pa) ; else if(a[pa-1] > b[pb-1]) return findKth(a, n, Arrays.copyOfRange(b, pb, m) , m - pb , k - pb) ; return a[pa-1] ; } public static void main(String[] args) { System.out.println(new Solution().findMedianSortedArrays(new int[]{1,2,3}, new int[]{6,7})) ; }}
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 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]4Median 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
- 推荐系统
- BZOJ3209 花神的嘲讽计划I
- 排序算法,堆算法实现TopK
- python sqlalchemy中文
- 简单词法分析器实现
- leetcode-4Median of Two Sorted Arrays
- 继承HibernateDaoSupport后怎样获取session&&注入sessionFactory
- GVIM 多窗口编辑
- MIME type类型
- jQuery Validate验证框架详解
- Android 图片裁剪 超出内存
- Android实战简易教程-第二十枪(GridView长按显示删除)
- 转自博客园:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
- 单源最短路径----Dijkstra算法