给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

1.我的思路:直接用sort,时间复杂度应如图所示

 class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums = nums1+nums2
nums.sort()
n = len(nums)
if n % 2 == 0:
ans=(nums[int(n/2)]+nums[int((n/2)-1)])/2
else:
ans=nums[int(n/2)]
return ans
solution=Solution()
solution.findMedianSortedArrays([1,3],[2])

2.网上的思路:

时间复杂度和空间复杂度都是O(m + n),由于两个数组都是有序的,要找到中位数最好的方法就是将两个有序数组进行合并,创建一个大小为m+n的数组arr, 由后向前遍历,比较两个数组末尾元素的大小,将大的那个数存放到arr数组的尾部,然后大数数组的尾部下标向前移动,arr尾部下标也向前移动。直到某一个数组已经全部放进arr中,将剩下的那个数组全部拷贝进去,最后判断如果是奇数,则返回中间位的那个数,如果是偶数,返回中间两个数的平均数。

leetcode4:两个排序数组的中位数的更多相关文章

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

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

  2. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  3. [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 ...

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

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

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

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

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

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

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

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

  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. koa-passport实现本地验证

    安装 yarn add koa-passport passport-local 先看下passport.js登录策略,判断用户和密码 const passport = require('koa-pas ...

  2. Eclipse识别不了jsp中的${pageContxt.request.contextPath }

    按计划这周系统学下Struts,但是搭完框架后jsp页面识别不了${pageContxt.request.contextPath } So这篇就记录一下我是怎么解决这个问题的 不管是tomcat7.0 ...

  3. nginx出现404和403错误

    配置文件的路径usr/local/nginx/conf/nginx.conf

  4. Django之Form验证

    view.py 配置 from django import forms from django.forms import fields # Create your views here. class ...

  5. 36_react_ui_antd

    1:最流行的开源react ui组件库 1.1:material-ui(国外) 1.2:ant-design(推荐:国内蚂蚁金服) 2.如何使用 方式一(页面引入): 在<head>标签内 ...

  6. OrderBy排序和IComparer的使用

    https://www.cnblogs.com/May-day/p/7490334.html 一,OrderBy排序在MDSN中有两种使用方法,如下 1>第一种方法的使用,就是根据某个字段排序, ...

  7. Vue note 2

    1.异步加载组件 一般单页面的缺点是首屏加载比较慢,因为首屏会把所有所需静态资源全部加载,对于中大型项目来说这样可能不是很合理.初步采用异步组件的方式,配合webpack,组件内部可以采用: comp ...

  8. Adobe CC 下载地址

    Adobe CC 2015下载地址 Photoshop http://trials3.adobe.com/AdobeProducts/PHSP/16/win32/Photoshop_16_LS20_w ...

  9. java.lang.OutOfMemoryError:GC overhead limit exceeded解决方法

    异常如下:Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded 一.解 ...

  10. mysql判断表里面一个逗号分隔的字符串是否包含单个字符串、查询结果用逗号分隔

    1.mysql判断表里面一个逗号分隔的字符串是否包含单个字符串 : FIND_IN_SET select * from tablename where FIND_IN_SET(传的参数,匹配字段) 例 ...