题目来源


https://leetcode.com/problems/search-a-2d-matrix/

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.

题意分析


Input:  a matrix and a target to query

Output: True or False

Conditions:在矩阵中查找元素,注意矩阵是有序的


题目思路


先在列二分查找定位哪一列,然后再行二分查找,注意边界条件

PS:其实直接在列查找的时候,发现low-1小于0时,直接返回False就可以了,但是所贴代码可以移植到插入元素的二分排序里面,为了统一就没有改动了。


AC代码(Python)


 __author__ = 'YE'

 class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
m = len(matrix)
n = len(matrix[0])
low = 0
high = m - 1 while low <= high:
mid = (low + high) / 2
if matrix[mid][0] == target:
return True
elif matrix[mid][0] > target:
high = mid - 1
else:
low = mid + 1
row = low - 1
if row < 0:
row = 0 low = 0
high = n - 1 while low <= high:
mid = (low + high) / 2
if matrix[row][mid] == target:
return True
elif matrix[row][mid] > target:
high = mid - 1
else:
low = mid + 1
return False matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]]
target = 16
print(Solution().searchMatrix(matrix,target))

[LeetCode]题解(python):074-Search a 2D Matrix的更多相关文章

  1. Leetcode 74 and 240. Search a 2D matrix I and II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. Java for LeetCode 074 Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. Leetcode 074 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. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  5. LeetCode(74) Search a 2D Matrix

    题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...

  6. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  7. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  9. LeetCode Search a 2D Matrix II

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

  10. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

随机推荐

  1. linux下查看一个进程的启动时间和运行时间

    使用 ps命令 :#ps -A -opid,stime,etime,args ps -A -opid,stime,etime,args 结果: root:src# ps -A -opid,stime, ...

  2. 分支语句:if

    (1)分支语句if: if(判断条件) { 满足条件要执行的语句(若满足则alert输出(“”)) } else { 不满足条件时执行的语句 } (若if满足,else绝对不走,反之,走else) 例 ...

  3. sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"问题解决

    安装一个软件时,遇到这个问题sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 上网 ...

  4. [kuangbin带你飞]专题八 生成树 - 次小生成树部分

    百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...

  5. [办公自动化]无法使用江南天安usbkey 无法使用视频网站

    同事打来电话说,无法使用江南天安开发的usbkey. 修复基本步骤记录如下: 1.卸载一切设备管理器中与之相关的驱动.拔出key. 2.重启计算机. 3.前往业务公开网站安装驱动. 4.插入key测试 ...

  6. Shader Model 版本与DirectX的关系(OpenGL对应的呢?)

    http://blog.sina.com.cn/s/blog_6e521a600100q4dr.html DX9还是能支持到固定管线的,虽然说是在内部被转换成shader: DX10明确不再支持固定管 ...

  7. 34. 求e的近似值

    求e的近似值 #include <stdio.h> double fact (int n); int main() { int i, n; double item, sum; while ...

  8. PHP不仅仅是PHP

    PHP不仅仅是PHP   PHP不仅仅是PHP. PHP的面试不仅仅会问到PHP语言本身(基本都是基础和细节),下面列举的主要是都是高级工程师的要求 比如:PHP中include和require的区别 ...

  9. mysql默认字符集修改

    (1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值,添加 [mysql] default-character-set = utf8 [mysqld] character_se ...

  10. sql server 数据库连接配置在外面 如何读取

    注意:web运行读取要把配置文件放在WEB-INF下面 Driver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver:// ...