leetcode 【Search a 2D Matrix 】python 实现
题目:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3
, return true
.
代码:oj测试通过 Runtime: 75 ms
class Solution:
# @param matrix, a list of lists of integers
# @param target, an integer
# @return a boolean
def searchInline(self, line, target):
start = 0
end = len(line)-1
while start <= end :
if start == end :
return [False,True][line[start]==target]
if start+1 == end :
if line[start]==target or line[end]==target :
return True
else:
return False
mid=(start+end)/2
if line[mid]==target :
return True
elif line[mid]>target :
end = mid-1
else :
start = mid+1 def searchMatrix(self, matrix, target):
if len(matrix) == 0 :
return False if len(matrix) == 1 :
return self.searchInline(matrix[0], target) if len(matrix) == 2 :
return self.searchInline([matrix[1],matrix[0]][matrix[1][0]>target], target)\ start = 0
end = len(matrix)-1
while start <= end :
if start == end:
return self.searchInline(matrix[start],target)
if start+1 == end:
if matrix[start][0] <= target and matrix[end][0] > target:
return self.searchInline(matrix[start],target)
if matrix[end][0] < target :
return self.searchInline(matrix[end],target)
mid = (start+end+1)/2
if matrix[mid][0] <= target and matrix[mid+1][0] > target:
return self.searchInline(matrix[mid],target)
elif matrix[mid][0] > target :
end = mid-1
else :
start = mid+1
思路:
先按行二分查找,再按列二分查找。
代码写的比较繁琐。
leetcode 【Search a 2D Matrix 】python 实现的更多相关文章
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- 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 -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- LeetCode——Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 二分搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- 汇编:jmp系列跳转指令总结
助记方法: J:跳转C: 进位位置位N: 否S: 符号位置位o: 溢出位置位Z: 零标志位置位E: 等于P:奇偶位置位A: AboveB: BelowL: Less (Little的比较级)G: Gr ...
- Dll注入:x86/X64 SetThreadContext 注入
在<Windows核心编程>第七章说到了线程优先级等知识,其中谈到了ThreadContext线程上下背景文. 其中介绍了GetThreadContext函数来查看线程内核对象的内部,并获 ...
- JavaScript:理解Promise方法
什么是promise? Promise的核心思想是代表异步操作的一个结果,并且promise具有三个状态(pending初始状态,fulfilled成功状态,rejected失败状态).我们可以理解为 ...
- C#中?和??用法
在C#中“?”有三种用法. 1.可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空,例如:string str=null;是正确的.int i= ...
- myeclipse引入工程后运行出错
An internal error occurred during: Launching efax on Tomcat 7.x . 项目运行时报错 因为你项目建的时候用的是Tomcat5.x 服务器 ...
- UsbKey开发
http://slf-1983.blog.163.com/blog/static/2990236320121113113955119/
- ajax400错误
在用ajax向后台传递参数时,页面一直显示错误400 bad request. 出现这个问题的原因是,要传递的VO类里一个实体bean里面的两个字段名称与前台表单序列化之后的name名称不匹配. 解决 ...
- 入门学习Linux常用必会命令实例详解
Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正理解Linux系统, ...
- linux三剑客之sed深度实践
参数: -a:追加文本到指定行后 -i:插入文本到指定行前 1.单行增加 [root@redhat~]# sed ' 2a 6,f ' linux.tet 1,a 2,b 6,f 3,c 4 ...
- Ubuntu12.04下YouCompleteMe安装教程(部分)
1.通过源码编译安装VIM 开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载 ...