mycode   time limited

def searchMatrix(matrix, target):
def deal(data):
if not data:
return False
row = len(data)
col = len(data[0])
#print('row,col',row,col)
for r in range(row):
for c in range(col):
#print(r,c)
if data[r][c] == target:
return True
elif data[r][c] > target:
deal(data[r+1:][:c])
if deal(matrix) :return True
else:
return False
matrix = [
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
searchMatrix(matrix, 5)

参考:

1、while循环中问题的关键是,如何不断缩小搜索的范围? -- 从右上角or左下角开始是最好的,因为两个方向的变化是一个变大一个变小

class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix or not matrix[0]:
return False
rows = len(matrix)
cols = len(matrix[0])
row, col = 0, cols - 1
while True:
if row < rows and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
else:
col -= 1
else:
return False

2、巧用pyhton

#暴力方法
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
return any(target in row for row in matrix)

leetcode-mid-sorting and searching - 240. Search a 2D Matrix II -NO的更多相关文章

  1. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  2. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  3. 【刷题-LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  4. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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

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

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

  6. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  7. (medium)LeetCode 240.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 ...

  8. Leetcode 240. 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 ...

  9. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

随机推荐

  1. 未能将文件 bin\zh-CHS\Webdiyer.MvcPager.resources.dll 复制到 obj\Release\Package\PackageTmp\bin\zh-CHS\Webdiyer.MvcPager.resources.dll。 未能找到文件“bin\zh-CHS\Webdiyer.MvcPager.resources.dll”

    在bin下面会生成更dll相同名称的xml文件,可能是因为我之前把项目中的很多部分设置了从项目中排除,关于dll也提示复制的问题解决办法是直接把那些生成的xml文件删除.在发布时还会提示obj文件夹下 ...

  2. having函数,case when与order by

    having:用于筛选分组后的各组数据.聚合函数,和group by一起使用(where不能和聚合函数使用)group by放在order by前使用,放在之后报错SELECT user_id fro ...

  3. 剑指offer-8:链表中倒数第k个结点

    转:https://blog.csdn.net/yjw123456/article/details/81061541 一.解题思路 两个指针p1,p2,开始都指向头结点 * 先让p2走k步 * 然后p ...

  4. cookie Web Storage API

    https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API https://d ...

  5. 修改MIGO或者ML81N产生的会计凭证项目文本增强

    在程序:MM07MFF9_F_BELEG_ERGAENZEN下做隐式增强

  6. JAVA中如何定义自定义注解

    了解注解 注解是Java1.5,JDK5.0引用的技术,与类,接口,枚举处于同一层次 .它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来对这些元素进行说明,注释 . 在Java中,自带 ...

  7. 五、WebSocket 链接

    一.前端代码: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml& ...

  8. 初试 pyhton 简易采集

    一.安装软件(用eclispe 搭建好环境好,没有取省自动补全编写代码会很卡,最后选用sumblie) eclispe  用的windows 32 4.31 python  用的 4.3.3  下载地 ...

  9. 015-Zabbix自动发现和自动注册

      前言 对于监控服务器越来越多的情况,如果还单独一个一个添加,那效率也太低,因此就要实现批量添加监控服务器的操作,Zabbix提供两种批量自动监控的方式: 自动发现:由服务端主动发起,Zabbix ...

  10. 折半搜索+Hash表+状态压缩 | [Usaco2012 Open]Balanced Cow Subsets | BZOJ 2679 | Luogu SP11469

    题面:SP11469 SUBSET - Balanced Cow Subsets 题解: 对于任意一个数,它要么属于集合A,要么属于集合B,要么不选它.对应以上三种情况设置三个系数1.-1.0,于是将 ...