【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)).



题解:注意这里对median的理解,如果A,B合并后的序列有奇数个元素,那么中间元素就是下标为(a.length+b.length)/2的元素;而如果合并后的序列有偶数个元素,那么median是下标为(a.length+b.length)/2和(a.length+b.length)/2-1两个元素的平均数。

我们实现一个二分在有序两个数组中找第k小的数的函数,然后在主函数中,根据合并后数组元素个数的奇偶性调用这个函数。

首先来看这个在两个有序数组中找第k小的数的函数 private double findKth(int a[],int b[],int k,int a_start,int b_start){ 。它的原理如下图所示:

即:

if(a_mid < b_mid)
    return findKth(a, b, k-k/2, a_start+k/2, b_start);
else
    return findKth(a, b, k-k/2, a_start, b_start+k/2);

再来看求解函数 public double findMedianSortedArrays(int A[], int B[]) ,当A和B合并后元素个数为奇数的时候,我们直接调用 findKth(A, B, (B.length+A.length)/2+1, 0, 0) 找到第(A.length+B.length)/2的数就是中位数了。而当A和B的合并后元素个数为偶数的时候,我们要调用两次findKth分别找到第(A.length+B.length)/2和第(A.length+B.length)/2-1的数,然后求它们的平均数,即 (findKth(A, B, (A.length+B.length)/2, 0, 0) + findKth(A, B, (A.length+B.length)/2+1, 0, 0)) / 2.0 。

代码如下:

 1 public class Solution {
 2     private double findKth(int a[],int b[],int k,int a_start,int b_start){
 3         //if a is empty
 4         if(a_start >= a.length)
 5             return b[b_start+k-1];
 6         if(b_start >= b.length)
 7             return a[a_start+k-1];
 8
 9         if(k == 1)
10             return Math.min(a[a_start], b[b_start]);
11
12         int a_mid = a_start + k/2 -1 < a.length?a[a_start+k/2-1]:Integer.MAX_VALUE;
13         int b_mid = b_start + k/2 -1 < b.length?b[b_start+k/2-1]:Integer.MAX_VALUE;
14
15         if(a_mid < b_mid){
16             return findKth(a, b, k-k/2, a_start+k/2, b_start);
17         }
18         else
19             return findKth(a, b, k-k/2, a_start, b_start+k/2);
20
21     }
22     public double findMedianSortedArrays(int A[], int B[]) {
23         int len = A.length + B.length;
24         if(len % 2 == 0)
25             return (findKth(A, B, len/2, 0, 0) + findKth(A, B, len/2+1, 0, 0)) / 2.0;
26         else
27             return findKth(A, B, len/2+1, 0, 0);
28     }
29 }
时间: 2024-10-15 04:00:11

【leetcode刷题笔记】Median of Two Sorted Arrays的相关文章

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

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的有序数组(m,n可能为0),要求找出这两个数组的中位数.并且程序的时间复杂度必须不能超过O(log(m+n)). 这道题的

刷题4. Median of Two Sorted Arrays

一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题. 性能只有42%的样子,内存占用太多.还需要进一步优化!!! 二.这个题目,我自己实现 提交了2次: 第1次: Wrong Answer 第2次:终于对了 下面是我的完整代码实现,需要的拿去: #include<iostream> #include<vector> using nam

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题解:水题不解释 1 /** 2 * Definition for singly-linked list. 3 * str

【leetcode刷题笔记】Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【leetcode刷题笔记】Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归

【leetcode刷题笔记】Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

【leetcode刷题笔记】Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [