题目描述:

最佳方法:O(m+n) O(1)

class Solution:
def searchMatrix(self, matrix, target):
if not matrix :
return False row = len(matrix)
col = len(matrix[0])
i = 0
j = col-1
while i<row and j>=0:
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
j -= 1
else:
i += 1
return False

方法二:O(nlogn)*

class Solution:
def searchMatrix(self, matrix, target):
# an empty matrix obviously does not contain `target`
if not matrix:
return False def search_rec(left, up, right, down):
# this submatrix has no height or no width.
if left > right or up > down:
return False
# `target` is already larger than the largest element or smaller
# than the smallest element in this submatrix.
elif target < matrix[up][left] or target > matrix[down][right]:
return False mid = left + (right-left)//2 # Locate `row` such that matrix[row-1][mid] < target < matrix[row][mid]
row = up
while row <= down and matrix[row][mid] <= target:
if matrix[row][mid] == target:
return True
row += 1 return search_rec(left, row, mid-1, down) or search_rec(mid+1, up, right, row-1) return search_rec(0, 0, len(matrix[0])-1, len(matrix)-1)

最新文章

  1. POJ 1696 Space Ant(极角排序)
  2. PL/SQL连接查询数据报错时Dynamic Performance Tables not accessible
  3. ASP 调用dll(VB)及封装dll实例
  4. solr拼写检查代码逻辑
  5. 【QT相关】Qt Widgets Module
  6. VBA怎样统计同一类型的数据的总和
  7. BZOJ 3963: [WF2011]MachineWorks [CDQ分治 斜率优化DP]
  8. unity集成openinstall流程
  9. django 连接mangodb 操作
  10. 软件测试实验二----selenium、katalon、junit
  11. C# WebApi+Task+WebSocket实战项目演练(四)
  12. hadoop_spark伪分布式实验环境搭建和运行实例详细教程
  13. nginx入门二
  14. VSFTPD虚拟用户配置
  15. vue路由跳转的多种方式
  16. C++说明符和限定符
  17. Sophus VS2010编译不支持?C++11语法的缘故。那有没有不带C++11特性的Sophus版本呢?
  18. InputStream TO byte
  19. Java多线程具体解释
  20. HDU 5646

热门文章

  1. python调用tushare获取上市公司管理层人员薪酬和持股
  2. SDL系列之 - 用画直线的方法来画正弦曲线
  3. 装箱与拆箱(TDB)
  4. python--模块导入与执行
  5. activeMQ的回顾
  6. 15_TLB中的G属性
  7. Centos 安装php Imagick 扩展
  8. 34. Thread类的常用方法
  9. 每天进步一点点-Tesseract 文字识别
  10. The past is just a story we tell ourselves.