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 + ...
随机推荐
- C语言移位运算
移位运算有两种:>>(右移),<<(左移). a>>b表示将a的二进制值右移b位. a<<b 表示将a的二进制值左移 b位.要求 a和 b都是整型, b ...
- iSwifting社区【www.iSwifting.com】招聘版主
iSwifting社区[www.iSwifting.com]昨天找来几个主持人.我已经被任命为他们的任务领域. 他们有百度的project噢,还有cocoaChina问答区的活跃人物! 欢迎大家一起学 ...
- leetcode第27题--Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 谈Linux
新手谈Linux 目录: 什么是Linux? Linux与UNIX的区别 Linux与Windows比较 什么是Linux发布版? Linux应用领域 Linux版本的选择 怎么学习Linux? ...
- cygwin的安装使用
Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供的许多特性:以及 Bash shell 的改写 ...
- Http Pipeline详细分析(下)
Http Pipeline详细分析(下) 文章内容 接上面的章节,我们这篇要讲解的是Pipeline是执行的各种事件,我们知道,在自定义的HttpModule的Init方法里,我们可以添加自己的事件, ...
- 【IOS开发】基础
1.Objective-C 为 ANSI C 添加了下述语法和功能: 定义新的类 类和实例方法 方法调用(称为发消息) 属性声明(以及通过它们自动合成存取方法) 静态和动态类型化 块 (block), ...
- Html Agility Pack解析HTML页
文章来源:Html Agility Pack解析HTML页 现在,在不少应用场合中都希望做到数据抓取,特别是基于网页部分的抓取.其实网页抓取的过程实际上是通过编程的方法,去抓取不同网站网页后,再进行分 ...
- js 获取某年的某天是第几周
/** * 判断年份是否为润年 * * @param {Number} year */ function isLeapYear(year) { return (year % 400 == 0) || ...
- ios学习笔记之UIViewControl生命周期
提到UIViewcontrol,每个人都不会陌生吧!平时实际开发中,每天的实际开发应该都少不了它.学过android的各位亲,也对生命周期这四个字并不陌生,无论是activity,还是service, ...