【Leet Code】Median of Two Sorted Arrays

Median of Two Sorted Arrays

Total Accepted: 17932 Total
Submissions: 103927My Submissions

There are two sorted arrays A and B 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)).

注意这里,There are two sorted arrays(这是两个已经排好序的数组),一看到我就想到了归并排序法,没有使用递归调用,但是任然可以AC:

class Solution {
public:
	double findMedianSortedArrays(int A[], int m, int B[], int n)
	{
		int i = 0, j = 0, k = 0;
		double r;
		int* result = new int[m + n];
		while( i < m && j < n )
		{
			if( A[i] < B[j] )
			{
			    result[k++] = A[i++];
			}
			else
			{
			    result[k++] = B[j++];
			}
		}
		while( i < m )
		{
			result[k++] = A[i++];
		}
		while( j < n )
		{
		    result[k++] = B[j++];
		}
		if( 1 == (m + n)%2 )
		{
		    r = result[(m + n - 1) / 2];
		}
		else
		{
		    r = (double)( result[(m + n - 2) / 2] + result[(m + n) / 2] ) / 2;
		}
		delete [] result;
		return r;
	}
};

【Leet Code】Median of Two Sorted Arrays

时间: 2024-11-07 11:38:09

【Leet Code】Median of Two Sorted Arrays的相关文章

[leet code 4] Median of Two Sorted Arrays

1 题目 There are two sorted arrays A and B 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)). 2 思路 这题比较简单,实现的其实就是归并排序merge那个部分,另外,也让我知道了,算偶数 sum%2==0  与 sum/2==0是不一样的,后者在sum=1

【LeetCode OJ】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/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)). 解题思路:将两个有序数

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays 这道题确实有点难,想挺久,看别人答案也是不能一下子就明白.题目难度为Hard,原题如下: 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)). 给定两个已

【LeetCode】Median of Two Sorted Arrays (2 solutions)

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是m+n

【经典】Median of Two Sorted Arrays

题目:leetcode Median of Two Sorted Arrays There are two sorted arrays A and B 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)). 注意:当m+n为偶数时,此题的中位数是中间两个数的平均值. 分析: 1.令数组A始终为长度小

【LeetCode】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)). 问题分析 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,

【leedcode】 Median of Two Sorted Arrays

https://leetcode.com/problems/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)). Example 1: nums1 =