之前写过一篇,这是第二篇。上一篇用了多种编程语言来做,这一次是以学算法为主,所以打算都用python来完成。

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 median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

这道题比较难,参考了这篇文章http://blog.csdn.net/yutianzuijin/article/details/11499917/,然后用python写了程序

其中第k小数算法是关键。总结下思路,争取以后我也可以多发明些这类nb的算法。

主要是将求A得问题巧妙地转换为求B的问题,并且在逻辑上证明是合理的。

class Solution(object):

    def findKth(self, arr1, len1, arr2, len2, k):
#always assume that len1 is equal or smaller than len2
if len1 > len2:
return self.findKth(arr2, len2, arr1, len1, k)
if len1 == 0:
return arr2[k - 1]
if k == 1:
return min(arr1[0], arr2[0])
#divide k into two parts
pa = min(k / 2, len1)
pb = k - pa
if arr1[pa - 1] < arr2[pb - 1]:
return self.findKth(arr1[pa:], len1 - pa, arr2, len2, k - pa)
elif arr1[pa - 1] > arr2[pb - 1]:
return self.findKth(arr1, len1, arr2[pb:], len2 - pb, k - pb)
else:
return arr1[pa - 1] def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums1_len = len(nums1)
nums2_len = len(nums2)
total = nums1_len + nums2_len
if total & 1:
return self.findKth(nums1, nums1_len, nums2, nums2_len, total/2 + 1)
else:
ret1 = self.findKth(nums1, nums1_len, nums2, nums2_len, total/2)
ret2 = self.findKth(nums1, nums1_len, nums2,nums2_len, total/2 + 1)
return float((ret1 + ret2)) / 2
												

LeetCode题解 15题 第二篇的更多相关文章

  1. LeetCode第[15]题(Java):3Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  2. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  3. LeetCode题解【题2】:两数相加

    原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99. ...

  4. leetcode:1-5题代码整理

    以下是这段时间抽时间刷的前5题,都是自己想的解法,或许不是最优解,只是整理下,方便日后优化提升 1. Two Sum: class Solution: # @return a tuple, (inde ...

  5. leetcode第15题--3Sum

    Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  6. 《LeetBook》leetcode题解(15):3Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. leetcode第15题:三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  8. LeetCode 第15题-三数之和

    1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...

  9. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

随机推荐

  1. 通过 C# 代码操作 Google 日历

    原文:通过 C# 代码操作 Google 日历 本文主题 借助 Google .NET APIs Client Library,通过 C# 代码在 Google 日历中创建会议邀请. 本文背景 最近, ...

  2. Arcgis for JS实现台风运动路径与影像范围的显示

    首先,看看详细的效果: 初始化状态 绘制中 绘制完毕 首先,组织数据.我组织的数据是JSON的,数据的详细形式例如以下: 其次,实现思路. 1.加入显示路径. 依据起始点,生成polyline的JSO ...

  3. 《Visual Studio Magazine》2013年读者选择奖—界面框架类

    好消息!2013 Visual Studio Magazine读者选择奖已经正式揭晓了!据了解,截至今年此奖项已经评选了21次,非常值得.NET开发人员信赖和参考.此次评选共有400多个产品角逐28个 ...

  4. JJG 623-2005 电阻应变仪计量检定规程

    JJG 623-2005 电阻应变仪计量检定规程 点击下载 JJG533-2007标准模拟应变量校准器检定规程 点击下载 JJG 533-1988标准(里面含有一些更具体的电路图供参考)

  5. Oracle的SOME,ANY和ALL操作

    平时很少用的这几个操作,今天遇到了.于是又看了一下文档. SOME和ANY一样,是比较宽松的,类似于OR.满足其中任何一个都可以. ALL要求严格一些,类似于AND,必须全部满足才可以. 不能单独使用 ...

  6. oracle中intersect的用法

    和 UNION 指令类似, INTERSECT 也是对两个 SQL 语句所产生的结果做处理的.不同的地方是, UNION 基本上是一个 OR (如果这个值存在于第一句或是第二句,它就会被选出),而 I ...

  7. 【Oracle】-【ROWNUM与索引】-索引对ROWNUM检索的影响

    看到ASK TOM的一篇文章,挺有感触的. http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:32812348052 ...

  8. Plan : 破晓

    题记 : 不要因为走的太远而忘记自己为什么而出发. 1. 白书(算法竞赛入门经典)看完(每一句话都要读懂) 2. 每次听完课把当天内容复习完(自习室10点以后复习) 3. 微机实验要提前预习(把实验报 ...

  9. 4月13号的web标准化交流化-开端

    这是实习工作的开始,也是正式踏入北京之后去参加的第一个活动.也算是想着法的去融入这个圈子. 这两个分享都是基于nodejs的.nodejs从11年开始就开始红火.但是真正nodejs能用来干什么? 我 ...

  10. Oracle常用语句记录

    交集/差集/合集 select * from tb_a intersect minus union all select * from tb_b 条件分支 decode() 例如:搜索条件没有手机就查 ...