题目来源


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. 小明A+B[HDU2096]

    小明A+B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. object-c cocos2d-x 写程序时注意调试的技巧

    (1)写程序时最好在类的init函数中显示类名,表明现在在执行哪个类,样例代码 CCLOG(@"cocos2d: Using Director Type:%@", [self cl ...

  3. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

  4. COJ262 HDNOIP201206施工方案

    HDNOIP201206施工方案 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 c国边防军在边境某处的阵地是由n个地堡组成的. ...

  5. oracle系列--第五篇 PLSQL连接本地的Oracle数据库

    这篇blog主要是针对新手,我也是个新手:) 我们把oracle成功的安装在了我们的计算机上面,那我们如何才能将PLSQL developer连 接到本地的oracle呢? 首先,我们必须有下面步准备 ...

  6. linux中shell变量$#,$@,$*,$?,$$,$!,$_,$0,$1,$2的含义解释

    变量说明:    $$    Shell本身的PID(ProcessID)    $!    Shell最后运行的后台Process的PID    $?    最后运行的命令的结束代码(返回值)    ...

  7. 安装rkhunter

    #!/bin/bash if [ ! -d /soft ];thenmkdir /soft fiwhich rkhunterif [ $? -eq 0 ];then echo "rkhunt ...

  8. Servlet 编程 http请求类型

    HTTP协议的8种请求类型介绍 HTTP协议中共定义了八种方法或者叫“动作”来表明对Request-URI指定的资源的不同操作方式,具体介绍如下: OPTIONS:返回服务器针对特定资源所支持的HTT ...

  9. 如何删除 OpenStack Nova 僵尸实例

    转自:http://www.vpsee.com/2011/11/how-to-delete-a-openstack-nova-zombie-instance/ 前天强制重启一台 OpenStack N ...

  10. 配置Log4j(很详细)

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...