LeetCode题解 15题 第二篇
之前写过一篇,这是第二篇。上一篇用了多种编程语言来做,这一次是以学算法为主,所以打算都用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题 第二篇的更多相关文章
- 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 ...
- 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 ...
- LeetCode题解【题2】:两数相加
原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99. ...
- leetcode:1-5题代码整理
以下是这段时间抽时间刷的前5题,都是自己想的解法,或许不是最优解,只是整理下,方便日后优化提升 1. Two Sum: class Solution: # @return a tuple, (inde ...
- 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 ...
- 《LeetBook》leetcode题解(15):3Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode第15题:三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...
- 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 + ...
随机推荐
- ubuntu新内核不能用启动回滚到旧内核的方法
先看一看自己电脑上有哪些内核文件 merlin@tfAnalysis:~$ dpkg --get-selections|grep linux libselinux1:i386 install linu ...
- 等差数列6《MAC》
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXhzdGFycw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- jquery实现文字上下滚动效果
文字上下滚动是经常用到的js效果,这里介绍一种上下渐隐渐出的文字展现效果! 代码实现很简单,只需要引入jquery就可以. 代码如下: <!DOCTYPE> <head> &l ...
- 华为JAVA(面试问题及答案节)
华为JAVA面试题 (后记:我没想到华为面试题是不寻常,,至少对我这种鸟来说是这样.对我个人来说.看看这样的题.可能比看<Think In Java>都还要好.因为这里面有很多的东西,都是 ...
- 处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决办法:以管理员运行命令:C: ...
- ADFS 2.0 配置简介 PartⅠ – 安装ADFS
Active Directory Federation Service 也即联盟身份认证服务是微软的单点登录解决方案 (SSO),配置步骤相当多,中文资料也比较少,写在这里希望对后来人有所帮助 ...
- ODBC操作excel
//ODBC连接Excel public static void main(String[] args) { Connection conn = null; Statement stm = nul ...
- C++ 关联容器之map插入相同键元素与查找元素操作
一.插入相同键元素操作 (1)insert方法 在map中的键必须是唯一的,当想map中连续插入键相同但值不同的元素时,编译和运行时都不会发生任何错误,系统会忽略后面的对已存在的键的插入操作,如 ma ...
- SugarSync的API总结
SugarSync API App支持SugarSync网盘的前提: 1.AccessKeyID:xxx 2.Private Access Key:xxx 3.AppID:xxx 详细的API总结如下 ...
- 新认识:SDF数据库
新认识:SDF数据库 一.SDF数据库初探 SDF是一个标准缩略数据库格式.这个数据库包含扩展名为.sdf的文件并且以结构化文件格式进行数据存储.这些SDF文件通常用于在不同数据库应用之间移动数据.它 ...