题目

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 实现的更多相关文章

  1. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

  10. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

随机推荐

  1. EPSG:4326

    简单说,"EPSG:4326"指的就是WGS84坐标系 参考 http://blog.csdn.net/cloverwindy/article/details/8663968 AU ...

  2. python super用法

    普通继承 class FooParent(object): def __init__(self): self.parent = 'I\'m the parent.' print 'Parent' de ...

  3. mac 查看python安装路径

    1.terminal : input: which Python 2.terminal: input : python  --->import sys  ----> print sys.p ...

  4. ASP.NET的三种开发模式

    前言 ASP.NET 是一个免费的Web开发框架,是由微软在.NET Framework框架中所提供的,或者说ASP.NET是开发Web应用程序的类库,封装在System.Web.dll 文件中.AS ...

  5. pat乙级1059

    1.c++ 位数不够前面补零: printf("04d", i); 位数不够前面补空格(右对齐): printf("4d", i); 位数不够后面补空格(左对齐 ...

  6. idea单元测试junit

    参考文章地址地址:http://blog.csdn.net/u011138533/article/details/52165577 本文按以下顺序讲解JUnit4的使用 下载jar包 单元测试初体验 ...

  7. POJ 3666 Making the Grade(区间dp)

    修改序列变成非递减序列,使得目标函数最小.(这题数据有问题,只要求非递减 从左往右考虑,当前a[i]≥前一个数的取值,当固定前一个数的取值的时候我们希望前面操作的花费尽量小. 所以状态可以定义为dp[ ...

  8. html5shiv.js的作用是

    解析 html5shiv主要解决HTML5提出的新的元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式.让CSS 样式应用在未知元素上只需执行 document.cre ...

  9. 如何修改魔兽争霸war3分辨率

    如何修改魔兽争霸war3 分辨率 有时候发现老电脑从XP系统升级到WIN7之后,发现玩魔兽不能全屏了(2边会有一些黑屏的).最后检查发现是魔兽在安装注册表之后显示的分辨率跟电脑的实际分辨率不同导致的. ...

  10. Oracle 函数 之 wm_concat()

    wm_concat() 把列转换成一行一列显示,使用wm_concat函数可以显示在一行一列. --1 建表 create table province_city ( province varchar ...