题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了。

题目一:

题目二:

解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值。但是不管怎么样我们可以确定的是,如果某一行的最小值都比target要大,那么这一行之后的值都比target要大。如果target介于某一行的最小值和最大值之间,那么target有可能在这一行。至于如何判断target是否存在,因为数组有序,用二分查找即可。

代码如下:

class Solution(object):
def searchMatrix(self, matrix, target):
import bisect
ROW = len(matrix)
if ROW == 0:
return False
COLUMN = len(matrix[0])
if COLUMN == 0:
return False
for i in matrix:
if i[0] <= target and i[-1] >= target:
inx = bisect.bisect_left(i,target)
if i[inx] == target:
return True
elif i[0] > target:
break
return False

【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II的更多相关文章

  1. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. 【leetcode】74. 搜索二维矩阵

    题目链接:传送门 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  3. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

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

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

  5. 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)

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

  6. 【LeetCode】108. Convert Sorted Array to Binary Search Tree

    Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...

  7. 【Leetcode】109. Convert Sorted List to Binary Search Tree

    Question: Given a singly linked list where elements are sorted in ascending order, convert it to a h ...

  8. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. springboot文件上传报错

    异常信息: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet reques ...

  2. Jenkins Html Rport 使用frame报错解决办法

    对于Blocked script execution in '<URL>' because the document's frame is sandboxed and the 'allow ...

  3. [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall

    SUMMARY: This article provides information on how to manually generate a new system self-signed cert ...

  4. SIRIM上海,http://www.sirim-global.com

    SIRIM上海 http://www.sirim-global.com

  5. [转帖]Linux shell中2>&1的含义解释 (全网最全,看完就懂)

    Linux shell中2>&1的含义解释 (全网最全,看完就懂) https://blog.csdn.net/zhaominpro/article/details/82630528   ...

  6. SET ANSI_NULL ON 和 SET QUOTED_IDENTIFIFR ON

    本文转自:https://blog.csdn.net/qq112212qq/article/details/84578263 SET ANSI_NULL ON : 判断非空:where colunm ...

  7. 获取kafka最新offset-scala

    无论是在spark streaming消费kafka,或是监控kafka的数据时,我们经常会需要知道offset最新情况 kafka数据的topic基于分区,并且通过每个partition的主分区可以 ...

  8. Vim实用技巧(一)

    vim 命令按键规定 标记 含义 x 按一次 x dw 按一次 d, w dap 按一次 d, a, p 同时按 和 n g<C-]> 按 g, 然后同时按 和 ] <C-=> ...

  9. neo4j 初探

    neo4j 初探 参考 转载:http://shomy.top/2018/06/08/neo4j-start/ 近期需要处理图数据,考察后打算使用neo4j, 相比其他一些图数据库,neo4j开源,跨 ...

  10. 【问题解决方案】CentOS7替换yum的问题:使用yum makecache出现File contains no section headers

    参考链接 CentOS7替换yum的问题:使用yum时出现File contains no section headers centos安装网络repo源及错误说明 一.centos替换yum的步骤 ...