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 这道题给定两个由小到大已排好序的列表,将它继续由从小到大的顺序排列,并返回新列表的中间值。(要求时间复杂度不能超过O(log (m+n))) 首先,给定的两个列表都是已经排好序的,所以我们不使用几种排序算法,他们的时间复杂度都超过了log(m+n)。我们可以使用数据结构中学到的归并(MergeList)两个已排好序的链表的方法。
 #-*-coding:utf-8-*-

 class Node(object):
'''
定义链表的结点
'''
def __init__(self,val):
self.val = val
self.next = None class Solution(object):
def initlist(self,nums):
'''
链表转换函数,传入一个列表,返回列表中元素组成的有头结点的链表
'''
l = Node(0)
ll = l
for num in nums:
node = Node(num)
ll.next = node
ll = ll.next
return l def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
list1 = self.initlist(nums1)
list2 = self.initlist(nums2)
list3 = Node(0)
pa = list1.next
pb = list2.next
pc = list3 while(pa and pb):
if(pa.val<=pb.val):
pc.next = Node(pa.val)
pc = pc.next
pa = pa.next
else:
pc.next = Node(pb.val)
pc = pc.next
pb = pb.next # 插入剩余段
if pa:
pc.next = pa
else:
pc.next = pb # 将已排好序的链表list3转换为列表nums3
nums3 = []
ll = list3.next
while(ll):
nums3.append(ll.val)
ll = ll.next j = len(nums3)/2
if len(nums3)%2==0:
return (nums3[j]+nums3[j-1])/2.0
else:
return nums3[j]

Leetcode4:Median of Two Sorted Arrays@Python的更多相关文章

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

  2. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

  3. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

  4. leetcode4 Median of Two Sorted Arrays学习记录

    学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...

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

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

  6. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

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

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  8. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

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

随机推荐

  1. Torch 日志文件的保存 logroll

    Torch 日志文件的保存 logroll 怎样将 Torch 在终端显示的信息,保存到 log 文件中 ?   现在介绍一种方法:利用 logroll 的方式.  参考 https://github ...

  2. SSR三网免流一键包以及使用教程

    这篇文章只是传统的SSR免流教程 首先拿到一台vps,可以是centos,可以是debian,也可以是ubuntu 使用xshell连接vps 执行一键安装包 wget http://104.224. ...

  3. java 线程安全 synchronized

    一.线程安全问题: 并发编程的原则:设计并发编程的目的是为了使程序获得更高的执行效率,但绝不能出现数据一致性(数据准确)问题,如果并发程序连最基本的执行结果准确性都无法保证,那并发编程就没有任何意义. ...

  4. Who is using Asio?

    https://think-async.com/Asio/WhoIsUsingAsio

  5. Java基础--Java语言介绍

    Java是一种半编译半解释的语言.1.编译阶段使用指令:javac这个阶段,Java编译器(也就是JVM:java虚拟机,后称JVM),首先把你的代码加载完成,然后开始检查语法.此后会产生.class ...

  6. UIWebView的使用

    iOS中UIWebView的使用详解 一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种 ...

  7. iPhone4下window各个部分的高度

  8. InstallShield打包设置相对路径

    InstallShield打包设置相对路径 在使用Installshield 打包安装文件时,添加打包文件时默认使用绝对路径,但是工程文件转移时(复制到其它位置时)编译时就会找不到安装文件,这样很不方 ...

  9. ios启动载入启动图片

    版本判断: 1.首先你要知道这个键值对的key:id key =   (id)kCFBundleVersionKey; 2.同过本地的NSBundle取得当前的版本号. 3.在沙盒中取得对应的版本号. ...

  10. eclispe---快捷键设置

    1,eclipse自动补全的设置(自动提示) 2014-11-03 14:51:43 如果你用过Visual Studio的自动补全功能后,再来用eclipse的自动补全功能,相信大家会有些许失望. ...