leetcode-mid-sorting and searching - 240. Search a 2D Matrix II -NO
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的更多相关文章
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【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 ...
- 【刷题-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 ...
- [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 ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- (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 ...
- 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 ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
随机推荐
- springboot热部署设置
springboot提供了热部署,所谓热部署就是当你修改了文件代码,不用重新去启动服务器,而你只要重新build一下当前项目就可以重新编译了.而这就是热部署. 其实springboot热部署就是通过一 ...
- the server responsed width a status of 404 (Not Found)
最近使用VS code写代码,使用Beautify插件格式化代码后,报以下错误: 反复查看代码,初始感觉没什么问题,有点懵.. 随着时间的推移,后来果然发现问题所在: 在加载模块的地方,多出了个空格( ...
- jQuery改变元素class属性
//去掉class属性 $(this).parent('li').removeClass("prev_selected"); //去掉同兄弟的class属性. $(this).pa ...
- 解决引入 lombok 注解不生效,Eclipse与IDEA集成 lombok
Eclipse -javaagent:lombok.jar -vmargs -javaagent:lombok.jar IDEA 添加依赖 <!--lombok 实体类注解--> < ...
- ubuntu apache 一个ip绑定多个域名,发布目录
1.将www.aaa.com 与 www.bbb.com 的DNS解析到你的服务器上 2.添加两个发布目录 /var/www/html/aaa /var/www/html/bbb 3.修改配置文件. ...
- id - 显示真实和有效的 UID 和 GID
总览 (SYNOPSIS) id [OPTION]... [USERNAME] 描述 (DESCRIPTION) 显示 USERNAME 或者 当前 用户 的 信息. -a 忽略, 同 其它 版本 兼 ...
- Qualcomm_Mobile_OpenCL.pdf 翻译-8-kernel性能优化
这章将会说明一些kernel优化的小技巧. 8.1 kernel合并或者拆分 一个复杂的应用程序可能包含很多步骤.对于OpenCL的移植性和优化,可能会问需要开发有多少个kernel.这个问题很难回答 ...
- Apache 80跳转443
<VirtualHost *:> ServerName your.domain.com #域名 RewriteEngine on #启用重定向 RewriteCond %{SERVER_P ...
- 查个远程桌面是否开启 debug版360报毒 release 不报毒
360 真tm流氓一个
- http知识总结
layout: '''http' title: 知识整理' date: 2019-06-09 17:07:20 tags: --- 简介 超文本传输协议(HTTP)是用于传输诸如HTML的超媒体文 ...