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

example:

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

简单来说,这道题的要求就是求两个数组的中位数,因为最近一直在学习Python,所以给大家展现下我自己的想法,希望大家互相交流。
 
class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        #连接两个数组
        nums3 = nums1 + nums2
        #数组排序
        nums3.sort()
        #初始化中位数的值为0
        med = 0
        index = index1 = index2 = 0
        lenofnums = len(nums3)
        if((lenofnums % 2) == 0):
            #如果为偶数个则取中间两个数的平均值
            index1 = int(lenofnums / 2 - 1)
            index2 = int(index1 + 1)
            med = (nums3[index1] + nums3[index2]) / 2
        else:
            #如果为奇数个直接取中间值
            index = int((lenofnums + 1) / 2 - 1)
            med = nums3[index]
        return med

  



原文地址:https://www.cnblogs.com/jiandanqinxin/p/8621324.html

时间: 2024-08-06 18:21:59

LeetCode第四题Median of Two Sorted Arrays解法的相关文章

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

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: 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

Python语言,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)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

【LeetCode OJ 004】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)). 解题思路:将两个有序数

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. 由于python没有事先定义整数还是浮点数,导致错误 2. class Solution: # @return a flo

leetcode个人题解——#4 Median of Two Sorted Arrays

题目描述:寻找两个有序数组合并后的中位数,要求算法时间复杂度为O(log(m+n)) 参考官方题解. 说到中位数,两个序列合并后的中位数下标一定是m+n+1/2或中间两位数的平均数. 我们只需要不断地划分两个序列直到找到答案即可,划分序列可以用二分法, left_part | right_part A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1] B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n

4. Median of Two Sorted Arrays(topK-logk)

4. 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 = [1, 3] nums2 = [2] The me

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)). 解题思路:这道题要求两个已经排好