`
hcx2013
  • 浏览: 88717 次
社区版块
存档分类
最新评论

Median of Two Sorted Arrays

 
阅读更多

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

 

 

public class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    	int len1 = nums1.length;
    	int len2 = nums2.length;
    	if (len1 == 0 && len2%2==0) {
    		return (nums2[len2/2-1]+nums2[len2/2])/2.0;
    	}
    	if (len1 == 0 && len2%2==1) {
    		return nums2[len2/2];
    	}
    	if (len2 == 0 && len1%2==0) {
    		return (nums1[len1/2-1]+nums1[len1/2])/2.0;
    	}
    	if (len2 == 0 && len1%2==1) {
    		return nums1[len1/2];
    	}
    	int mid = (len1+len2)/2;
    	if ((len1+len2)%2 == 0) {
    	return (findKthNum(nums1, nums2, mid)+findKthNum(nums1, nums2, mid+1))/2.0;
    	} else {
    	return findKthNum(nums1, nums2, mid+1);
    	}
    }
    public static int findKthNum(int[] arr1, int[] arr2, int kth) {
		if (arr1 == null || arr2 == null) {
			throw new RuntimeException("Your arr is invalid!");
		}
		if (kth < 1 || kth > arr1.length + arr2.length) {
			throw new RuntimeException("K is invalid!");
		}
		int[] longs = arr1.length >= arr2.length ? arr1 : arr2;
		int[] shorts = arr1.length < arr2.length ? arr1 : arr2;
		int l = longs.length;
		int s = shorts.length;
		if (kth <= s) {
			return getUpMedian(shorts, 0, kth - 1, longs, 0, kth - 1);
		}
		if (kth > l) {
			if (shorts[kth - l - 1] >= longs[l - 1]) {
				return shorts[kth - l - 1];
			}
			if (longs[kth - s - 1] >= shorts[s - 1]) {
				return longs[kth - s - 1];
			}
			return getUpMedian(shorts, kth - l, s - 1, longs, kth - s, l - 1);
		}
		if (longs[kth - s - 1] >= shorts[s - 1]) {
			return longs[kth - s - 1];
		}
		return getUpMedian(shorts, 0, s - 1, longs, kth - s, kth - 1);
	}

	public static int getUpMedian(int[] a1, int s1, int e1, int[] a2, int s2, int e2) {
		int mid1 = 0;
		int mid2 = 0;
		int offset = 0;
		while (s1 < e1) {
			mid1 = (s1 + e1) / 2;
			mid2 = (s2 + e2) / 2;
			offset = ((e1 - s1 + 1) & 1) ^ 1;
			if (a1[mid1] > a2[mid2]) {
				e1 = mid1;
				s2 = mid2 + offset;
			} else if (a1[mid1] < a2[mid2]) {
				s1 = mid1 + offset;
				e2 = mid2;
			} else {
				return a1[mid1];
			}
		}
		return Math.min(a1[s1], a2[s2]);
	}
}

 

 

0
2
分享到:
评论

相关推荐

    LeetCode4 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. ...Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java AC版本

    java-leetcode题解之Median of Two Sorted Arrays.java

    java java_leetcode题解之Median of Two Sorted Arrays.java

    js代码-4. Median of Two Sorted Arrays

    在JavaScript编程领域,"Median of Two Sorted Arrays"(两个排序数组的中位数)是一个经典的算法问题。这个问题的目的是在给定两个已排序的数组中找到它们合并后的中位数,但无需实际合并这两个数组。这是一个典型...

    leetcode无法登录-MedianOfTwoSortedArrays:双排序数组的中位数

    leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 ...

    程序员面试宝典LeetCode刷题手册

    4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...

    median-of-two-sorted-arrays

    Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4...

    Coding Interview In Java

    7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two ...

    leetcodepython001-algorithm:leetcode问题(cpp、java、python),书籍破解_the_coding

    Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. Median of Two Sorted Arrays 004. Median of Two Sorted Arrays 005. Longest Palindromic Substring 006. ZigZag ...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating Characters.cpp │  │  ├── 004 - Median of Two Sorted Arrays.cpp │  │  └──...

    java-leetcode题解之004-Median-of-Two-Sorted-Arrays

    java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays

    c语言-leetcode题解04-median-of-two-sorted-arrays.c

    c语言入门 c语言_leetcode题解04-median-of-two-sorted-arrays.c

    js-leetcode题解之4-median-of-two-sorted-arrays.js

    js js_leetcode题解之4-median-of-two-sorted-arrays.js

    lrucacheleetcode-leetcode:leetcode

    Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix ...

    leetcode双人赛-LeetCode:力扣笔记

    Median of Two Sorted Arrays 困难 数学 Longest Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 ...

    LeetCode 刷题汇总1

    * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...

    leetcode分类-leetcode:leetcode问题的代码

    #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:...

    LeetCode题目集1

    4. **Median of Two Sorted Arrays** (Hard) 这是一道查找两个已排序数组中中位数的题目。要求整体时间复杂度为O(log (m+n))。可以采用二分查找的方法,不断缩小查找范围,找到两个数组合并后中间位置的元素,或者...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    Median of Two Sorted Arrays 30.7% Hard 0005 Longest Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium ...

    10个最常见的Java算法.doc

    * 经典问题:Evaluate Reverse Polish Notation, Longest Palindromic Substring, 单词分割, 字梯, Median of Two Sorted Arrays, 正则表达式匹配, 合并间隔, 插入间隔, Two Sum, 3Sum, 4Sum, 3Sum Closest, String ...

Global site tag (gtag.js) - Google Analytics