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")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
随机推荐
- android下文件下载
public static void downFile(final String url){ new Thread(){ public void run(){ FileOutputStream os= ...
- 访问Tableau自带的PostgreSQL数据库
突然发现公司Tableau服务器的数据库大小急剧增加,因此决定直接连上数据库排查.过程记录如下:最后发现有个http_requests 表体积巨大(7G),本来以为是数据缓存什么的.结果是日志问题o( ...
- ubuntu中彻底删除nginx
1.先执行一下命令: 1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get ...
- SpringMVC 详解
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- 添加站点图标: 为SAE上的WordPress站点添加自己的Favicon
由于插件 Jetpack的"添加站点图标"功能有问题, 无法从本地上传ico文件到SAE的Storage. 因此需要手动添加站点图标. Step 1: 制作或下载自己的ico文件, ...
- TortoiseSVN和Eclipse中subversion无法协同使用
环境: Eclipse版本Luna, 第一次安装subversion插件时, 使用了http://download.eclipse.org/releases/luna中的Subversive, 版本为 ...
- 浏览器对body节点scrollTop解析的差异
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style t ...
- EAN-13 条码(又称GTIN-13 条码)
EAN全名为European Article Number(欧洲商品条码),在1977年时由欧洲几个主要工业国家共同发展出来的,后来变成国际商品条码系统.台湾在1985年加入EAN会员,现在我们买东西 ...
- 质因数分解的rho以及miller-rabin
一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...
- 数据结构——栈(Stacks)
栈遵循LIFO ( last in first out) 即后入先出原则 栈结构类似于叠盘子 后叠上去的要先拿走 才能拿到下面的盘子 因此stack是一种访问受限的线性存储结构 用单向链表的结构来存储 ...