class Solution:
# @param matrix, a list of lists of integers
# @param target, an integer
# @return a boolean
def searchMatrix(self, matrix, target):
n=len(matrix)
m=len(matrix[0])
if target>matrix[n-1][m-1] or target<matrix[0][0]:
return False
head = 0
end = n-1
mid = (end+head)//2
while mid>head and mid <end:
if target<matrix[mid][0]: end=mid
else: head=mid
mid= (end+head)//2
keyline=mid
if target>matrix[keyline][m-1]: keyline+=1
head = 0
end = m-1
mid = (end+head)//2
while mid>=head and mid <end:
if target<=matrix[keyline][mid]: end=mid
else: head=mid+1
mid= (end+head)//2
return target==matrix[keyline][mid] if __name__=='__main__':
s=Solution()
m=[[1,2,3],[4,5,6],[7,8,9]]
print(s.searchMatrix(m,3))

OJ runtime :224 ms

Search a 2D Matrix【python】的更多相关文章

  1. 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...

  2. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  4. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  6. 【刷题-LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  7. 【python】ipython与python的区别

    [python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼   分类: Python http://mba.shengwushibie.com/itbook ...

  8. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

随机推荐

  1. java项目组会议纪要

    上周五项目经理开例会让我记录会议纪要,以下是我记录的纪要.给大家分享一下! 一.时间:2014年04月25日 二.地点:研发部 三.人物:xx,xx,xx 四.内容(相关项目的一些事项): 1.对待需 ...

  2. UVa----------1594(Ducci Sequence)

    题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...

  3. QT VS检测内存泄漏

    测试程序:http://download.csdn.net/detail/ajaxhe/4085447 vld-2.2.3: http://vld.codeplex.com/releases/view ...

  4. Linux malloc大内存的方法

    本博文为原创,遵循CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/9963199 ------------------ ...

  5. UVA 10341 Solve It 解方程 二分查找+精度

    题意:给出一个式子以及里面的常量,求出范围为[0,1]的解,精度要求为小数点后4为. 二分暴力查找即可. e^(-n)可以用math.h里面的exp(-n)表示. 代码:(uva该题我老是出现Subm ...

  6. OpenWRT推理client线上的数

    有两种方法: 一. 经DHCP client通讯组列表 (缺点:client列表会依据超时时间刷新,一般超时时间为12h,) 二. 通过arp缓存列表/proc/net/arp(缺点:arp刷新时间默 ...

  7. [计算机网络] vsftpd的安装与使用

    简单介绍: vsftpd是一个能够执行在类UNIX操作系统上的FTPserver软件,它能够执行在Linux.BSD.Solaris.HP-UX等系统上. 1 vsftpd的安装 在ubuntu系统上 ...

  8. Android 启动APP时黑屏白屏的三个解决方案(转载)

    你会很奇怪,为什么有些app启动时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示,但是有些app却不会如QQ手机端,的确这里要做处理一下.这里先了解一下为什么会出现这样的现象,其实很简单 ...

  9. 随机生成器、thread(暂停)、清屏定义

    1.生成一个随机生成器 Random a = new Random(); //()可以填写随机生成器的种子,这个种子只能是整数(int) ); //()内的数字代表小于5的非负整数,包括零,例如0,1 ...

  10. ZSTU OJ 3999 零基础学算法---邻接表

    题目:Click here 题意:我就喜欢中文题! 分析:这个题虽然是中文题,但是还是有一点费解的.其实就是给你一棵树,是用图的形式给你的,只知道a,b之间有一条边,并不知道谁是父,谁是子.思路就是先 ...