No.4 Median of Two Sorted Arrays

4. Median of Two Sorted Arrays

  • Total Accepted: 104147
  • Total Submissions: 539044
  • Difficulty: 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)).

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5

  其实整个思路应该就是将两个有序数组进行排序的过程,然后根据总个数的奇偶决定中位数是一个还是两个的平均值。
 1 public class No_4 {
 2     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
 3         int total = nums1.length + nums2.length ;
 4         int [] res ;
 5         //判断最后的结果是单独一个数还是两个数的平均值
 6         if(total%2 == 1){
 7             res = new int [1] ;
 8         }else{
 9             res = new int [2] ;
10         }
11         int count = 0 ;    //当前值是第count小的数
12         int i = 0 ;        //nums1的下标
13         int j = 0 ;        //nums2的下标
14         int k = 0 ;       //res的下标
15         int min ;
16         /*
17          * 将两个有序数组进行排序的过程
18          * 当total为偶数时,结果为总数的第 (total-1)/2+1个数和第(total-1)/2+2个数
19          * 当total为基数时,结果为总数的第(total-1)/2+1个数
20          * 所以,当count > (total-1)/2 时,开始存储,直到res存满为止
21          */
22         while((i < nums1.length ) && (j < nums2.length) && (k < res.length)){
23             count++ ;
24             min = nums1[i] > nums2[j] ? nums2[j++] : nums1[i++] ;
25             if(count > (total-1)/2){
26                 res[k++] = min ;
27             }
28         }
29         //考虑nums2先排除完而没有完全得到需要的结果的情况
30         while((i < nums1.length ) && (k < res.length)){
31             count++ ;
32             min = nums1[i++] ;
33             if(count > (total-1)/2){
34                 res[k++] = min ;
35             }
36         }
37       //考虑nums1先排除完而没有完全得到需要的结果的情况
38         while((j < nums2.length ) && (k < res.length)){
39             count++ ;
40             min = nums2[j++] ;
41             if(count > (total-1)/2){
42                 res[k++] = min ;
43             }
44         }
45
46         //返回结果
47         if(total%2 == 1){
48             return res[0] ;
49         }else{
50             return ((res[0] + res[1])/2.0) ;
51         }
52     }
53 }
时间: 2024-07-30 13:36:25

No.4 Median of Two Sorted Arrays的相关文章

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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

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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

第三周 leetcode 4. Median of Two Sorted Arrays (HARD)

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: 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)). 解题思路:如果是一个已排序数组,求中位数,可以直接计算.因此比较常规的想法是直接将两个有序数组合并

【题解】【数组】【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

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

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)). 这道题的

[leetcode]Median of Two Sorted Arrays @ Python

原题地址:https://oj.leetcode.com/problems/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)). 解题思路:这道题要求两个已经排好

[LintCode] 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. Have you met this question in a real interview? Example Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5. Given A=[1,2,3] and B=[4,5],