Search a 2D Matrix【python】
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】的更多相关文章
- 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 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【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 ...
- 【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 ...
- 【刷题-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 ...
- 【python】ipython与python的区别
[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼ 分类: Python http://mba.shengwushibie.com/itbook ...
- [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 ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
随机推荐
- jquery 上下滑动效果
<script type="text/javascript"> var myar = setInterval('AutoScroll(".li_gundong ...
- CAN总线与RS485的比较
CAN总线与RS485的比较 http://blog.csdn.net/reille/article/details/6135546 can总线与485总线有什么区别? http://blog.16 ...
- UVa----------1594(Ducci Sequence)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- border-radius.htc为ie6-8实现圆角
~~圆角是比较常用的css3属性,但是ie6-8并不支持圆角,可用border-radius.htc html组件实现圆角, border-radius.htc内部应用vml进行了重绘 border ...
- 最详细最实用-Orcad10.5安装说明
接受协议 选择安装 忽略警告 全部为空 忽略警告 直接下一步 选择YES 为空,直接下一步 全选,根据需要修改该路径,下一步 根据需要修改该路径 下一步 直接next 忽略提示 直接下一步 直接下一步 ...
- C++ ofstream和ifstream详细用法
转载地址:http://soft.chinabyte.com/database/460/11433960.shtml ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就 ...
- Android最新源码4.3下载-教程 2013-11
Android最新源码4.3下载-教程 有的下载会出现问题: 需要 修改manifest.xml中的fetch: "git://Android.git.linaro.org/" ...
- 64位CentOS上编译 Hadoop 2.2.0
下载了Hadoop预编译好的二进制包,hadoop-2.2.0.tar.gz,启动起来后.总是出现这样的警告: WARN util.NativeCodeLoader: Unable to load n ...
- Android显示GIF动画完整示例(二)
MainActivity如下: package cc.testgif2; import android.os.Bundle; import android.app.Activity; /** * De ...
- 数据库 isnull()、nvl()、ifnull() 使用
SqlServer 插入字段值 为空 设置默认值 isnull(val_1,val_2) 当val_1为 null 的时候 返回 val_2的值 Oracle nvl(val_1,val_2) 当va ...