题目来源


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. 胜利大逃亡[HDU1253]

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. BZOJ3735 : [Pa2013]Konduktorzy

    二分一个最大的位置$x$,计算$t=\sum_{i=1}^k\lfloor\frac{x}{a_i}\rfloor$. 如果$t\leq n$,那么说明就算全部检票员都走到了这里,也不够$n$个指令, ...

  3. BZOJ3847 : ZCC loves march

    注意到集结操作相当于合并一些点 于是我们可以使用并查集 对于每一行.每一列维护一个链表,储存里面的点 查询x时,在并查集中找到x的祖先u,此时x的坐标就是u的坐标 然后扫描u所在行列的链表,依次删除每 ...

  4. eclipse生成jar包

    第一:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包. 1.在eclipse中选择你要导出的类或者package,右击,选择Export子选项: 2.在弹出的对话 ...

  5. 看看 JDK 8 给我们带来什么(转)

    世界正在缓慢而稳步的改变.这次改变给我们带来了一个新模样的JDK7,java社区也在一直期盼着在JDK8,也许是JDK9中出现一些其他的改进.JDK8的改进目标是填补JDK7实现中的一些空白——部分计 ...

  6. 使用distinct出现的一个问题

    如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中. 错误的写法:select distinct top 100  userphone  from m ...

  7. Java开发环境准备

    Java开发环境准备 这里主要讲JDK的配置,JDK的安装和安装一般的应用软件一样,下载JDK安装就可以了,但安装后主要是配置好才可用.我相信很多初学者和我刚开始一样,安装好JDK以后就直接点击桌面上 ...

  8. js判断时间差

    //var startDate = "2015-09-09"; //var endDate = "2015-09-08"; var startDate = &q ...

  9. php字符串常用函数

    addslashes print addslahes ('She said, "Great!"'); #output #She said, \"Great!\ echo ...

  10. backbone-todo案例分析

    todo案例可以到这个地址下载 https://github.com/jashkenas/backbone 添加数据后 todo案例不涉及Router,仅有Model.Collection.View的 ...