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")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
随机推荐
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- #, about:blank,javascript:路径比较
试了一下在<a>,<img>,<iframe>中用#,about:blank和javascript: 代码如下: <!Doctype html> < ...
- [翻译]Orchard-修改首页布局
前言 Orchard在你的站点应用的默认主题叫做”Theme Machine”.该主题包括CSS样式和一个布局框架. Orchard 允许你选择或排除显示在你站点每个页面的布局. 默认情况下下图中蓝色 ...
- 用jmeter进行多用户并发压力测试 [转]
近日manager要求对项目进行压力测试,开始对jmeter进行了研究.jmeter是Apache一个开源项目,可对各种项目进行测试,甚至包括junit. 测试要求如下,多用户同时登陆web应用程序, ...
- CSS3-旋转齿轮
CSS3-旋转齿轮 查看DEMO 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. 先来认识一下css3的animation animat ...
- Python之路:迭代器和yield生成器
一.迭代器 对于Python 列表的 for 循环,他的内部原理:查看下一个元素是否存在,如果存在,则取出,如果不存在,则报异常 StopIteration.(python内部对异常已处理) 使用迭代 ...
- poj 2513
http://poj.org/problem?id=2513 73348K 1438MS C++ 1614B解题思路:欧拉路的应用 要点 :1.判断连通性 ...
- 转: seajs手册与文档之 -- require规则
require 规则 正确拼写 不要修改 使用直接量 动态依赖的小提示 书写规则 使用 SeaJS 书写模块代码时,需要遵循一些简单规则: 1. 正确拼写 在模块代码中,第一个参数 必须 命名为 re ...
- 第三节 ISBN 码 / ISSN 码
ISBN与ISSNEAN的用途很广,除了我国的商品条码CAN以及日本商品条码JAN外,目前国际认可的书籍代号与期刊号的条码,也都是由EAN变身而来的.书籍的国际认可代号称为国际标准书号(Interna ...
- win32 api Windows窗口的创建
windows窗口的创建有以下几个步骤: 1.创建注册窗口类 2.创建窗口句柄 3.显示更新窗口 4.消息循环 1.创建注册窗口类 所谓创建窗口类就是定义一个WNDCLASS类对象,并将该对象进行初始 ...