4. 两个排序数组的中位数

问题描述

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. 直接用python自带的函数,将两个列表相加,生成一个新列表。对新列表进行排序,然后求中位数。
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
resultList = nums1 + nums2
resultList.sort()
numLength = len(resultList)
if numLength % 2 == 1:
index = int((numLength + 1) / 2) - 1
return resultList[index]
else:
index = int(numLength / 2) - 1
return (resultList[index] + resultList[index + 1]) / 2
  1. 由于两个列表自身有序,循环两个列表,将值较小的放入新列表中,把剩下的整个列表放到新列表中。则新列表有序,求中位数。
class Solution:
def findMedianSortedArrays0(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums3 = [0] * (len(nums1) + len(nums2)) r_i, l_i, i = 0, 0, 0
while (l_i < len(nums1)) and (r_i < len(nums2)):
if nums1[l_i] < nums2[r_i]:
nums3[i] = nums1[l_i]
l_i = l_i + 1
else:
nums3[i] = nums2[r_i]
r_i = r_i + 1 i += 1 if l_i != len(nums1):
nums3[i:] = nums1[l_i:]
else:
nums3[i:] = nums2[r_i:] len_3 = len(nums3)
if len_3 % 2 != 0:
return float(nums3[(len_3 - 1) // 2]) return (nums3[(len_3 - 1) // 2] + nums3[len_3 // 2]) / 2

结果思考

方法1要优于方法2,我认为可能是python提供的 sort方法的时间复杂度较低。

GitHub地址:https://github.com/protea-ban/LeetCode

LeetCode4. 两个排序数组的中位数的更多相关文章

  1. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

  2. [Swift]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 ...

  3. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  4. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

  5. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  6. Leetcode4--->求两个排序数组的中位数

    题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...

  7. leetcode4:两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 1.我的思路:直接用sort,时间复杂度应如 ...

  8. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  9. 从0打卡leetcode之day 5 ---两个排序数组的中位数

    前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...

随机推荐

  1. javascript实现新浪微博MID与地址转换

    新浪微博每一条微博都会有一个mid,然后每条微博都有一个独立的地址,例如:http://www.weibo.com//Bw3SXzWzP 规律:地址中的黄色部分是用户id,绿色部分是微博的识别字符串, ...

  2. jps, jinfo命令

    jps主要用来输出JVM中运行的进程状态信息. -q 不输出类名.Jar名和传入main方法的参数 -m 输出传入main方法的参数 -l 输出main类或Jar的全限名 -v 输出传入JVM的参数 ...

  3. python3-递归

    # Auther: Aaron Fan """递归特性:1. 必须有一个明确的结束条件2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少3. 递归效率不高,递 ...

  4. c# 常规验证基类

    using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions ...

  5. UIWebView分页显示

    問題:使用iOS UIWebView時,載入本地html檔案,但是該檔案太大,結果螢幕畫面形成一長條型顯示,雖然用滾動畫面可以看見整個html檔案,但是滑來滑去,不好用. 目標:用UIWebView載 ...

  6. 设计模式03: Builder 生成器模式(创建型模式)

    Builder生成器模式(创建型模式) Builder模式缘起假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分富于变化.如果使用最直观的设计方法,每个房屋部分的变化,都将 ...

  7. 15分XX秒后订单自动关闭(倒计时)

    //订单记录 function get_order(){ //请求订单ajax方法 XX.send_api("method",{data},function(){ var date ...

  8. HackFifteen 移除背景以提升Activity启动速度

    1.概要:     移除窗口默认背景是提升应用程序启动速度的一个简单技巧.判断是否需要移除背景的原则很简单:如果应用程序界面需要 占据窗口100%的控件,就需要将windowBackground属性设 ...

  9. laravel中firstOrCreate的使用

    laravel - firstOrCreate(判断是否存在, 不存在则新增数据) 1, 判断goods_name是否存在YKQ003213_G这个参数 2, 不存在则添加数组的内容 3, 需要设置自 ...

  10. VSCode调试设置

    tasks.json { "version": "0.1.0", "isShellCommand": true, "args&qu ...