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")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
随机推荐
- HDU1284钱币兑换问题( 母函数打表)
题意:在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. 原题http://acm.hdu.edu.cn/showproblem.php?pid=128 ...
- .net Windows服务程序和安装程序制作图解
最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里把自 ...
- 使用VS创建WebPart部件,并部署到SP(待修改)
http://www.cnblogs.com/mingmingruyuedlut/archive/2012/12/02/2789488.html
- quartz搭建与应用
1.添加依赖 依赖包括Quartz和logback <dependencies> <dependency> <groupId>org.quartz-schedule ...
- GIT简易使用流程
git是目前世界上最先进的分布式版本控制系统(摘自廖雪峰官网) 首先需要在系统上安装git: Windows系统在这下载: RHEL/Centos/Fedora用户可以用以下命令安装:yum -y i ...
- python3.4.3如何获取文件的路径
#coding:utf-8from tkinter import *from tkinter import filedialogroot = Tk()root.filename = filedialo ...
- mac下 配置 Apache Php Mysql
参考 http://www.guomii.com/posts/30136 参考 http://forums.mysql.com/read.php?11,600754,600754 MacOS 10.8 ...
- linux下观看b站视频,解决字体乱码
如图: 各种字体都显示为方块,解决办法也很简单. 点击视频右边的齿轮,也就是设置,更改字体. 默认的微软雅黑字体,一般换成其他的字体应该都能正常显示. 这是为更改后:
- Shiro入门(1)
=============基本概念=================== 什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是 ...
- Cloud Foundry中warden的网络设计实现——iptable规则配置
在Cloud Foundry v2版本号中,该平台使用warden技术来实现用户应用实例执行的资源控制与隔离. 简要的介绍下warden,就是dea_ng假设须要执行用户应用实例(本文暂不考虑ward ...