leetcode240 搜索二维矩阵 II
题目:
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
示例:
现有矩阵 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]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。
思路:
二分法。
- 先获取当前矩阵的最大值和最小值,即左上角的值和右下角的值
为(x1,y1)和(x2,y2)。当x1 = x2 且 y1 = y2时,说明矩阵为一个点。 - 求mid值,即 ( (x1+x2)/2 , (y1+y2)/2 )
- 将mid值与target进行比较,来决定接下来取查询那些范围
· 如果target = mid 说明找到了目标值
· 如果target < mid,说明以mid为最小值的那块矩阵,不会有target, target在其他范围
· 如果target > mid,说明以mid为最大值的那块矩阵里,不会有target, target在其他范围里面 - 接下来对其他范围进行递归查询

代码:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length < 1 || matrix[0] == null || matrix[0].length < 1) {
return false;
}
return searchMatrix(matrix,target,0,0,matrix.length-1,matrix[0].length-1);
}
//方法
private boolean searchMatrix(int[][] matrix, int target, int x1, int y1, int x2, int y2) {
if(x2 < x1 || y2 < y1){
return false;
}
if(target < matrix[x1][y1] || target > matrix[x2][y2]){//若果小于矩阵最小值,或者大于矩阵最大值,直接返回false。
return false;
}
int mid_x = (x1 + x2) / 2;
int mid_y = (y1 + y2) / 2;
if(target == matrix[mid_x][mid_y]){
return true;
}
if(target < matrix[mid_x][mid_y]){ //target不在第四象限
return (
//查找第二象限
searchMatrix(matrix,target,x1,y1,mid_x-1,mid_y-1) ||
//查找第一象限
searchMatrix(matrix,target,x1,mid_y,mid_x-1,y2) ||
//查找第三象限
searchMatrix(matrix,target,mid_x,y1,x2,mid_y-1)
);
}else { //target不在第二象限
return (
//查找第四象限
searchMatrix(matrix, target,mid_x+1,mid_y+1,x2,y2) ||
//查找第一象限
searchMatrix(matrix,target,x1,mid_y+1,mid_x,y2) ||
//查找第三象限
searchMatrix(matrix,target,mid_x+1,y1,x2,mid_y)
);
}
}
}
但是我看其他人提交的代码,思路是从左下 或者 右上开始遍历。
思路是:
从左下角角标开始查找
如果>target 则向上移动角标
如果<target 则向右移动角标
如果==target 则返回True
如果角标出界还没找到target 则返回False
但是我认为这种不是最优的,比如二维数组只有一行或者一列的话,这就是一次时间复杂度为O(n)的遍历。
代码如下(代码是从右上角开始的)
class Solution {
public boolean searchMatrix(int[][] matrix, int target){
if (matrix.length==0)
return false;
int i = matrix.length-1,j=0;
while(i>=0 && j<matrix[0].length){
if (matrix[i][j] == target)
return true;
else if(matrix[i][j]>target)
i--;
else if(matrix[i][j]<target)
j++;
}
return false;
}
}
leetcode240 搜索二维矩阵 II的更多相关文章
- leetcode-240搜索二维矩阵II
搜索二维矩阵II class Solution: def searchMatrix(self, matrix, target): """ :type matrix: Li ...
- [Swift]LeetCode240. 搜索二维矩阵 II | 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 ...
- lintcode:搜索二维矩阵II
题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...
- LintCode-38.搜索二维矩阵 II
搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复 ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- Java实现 LeetCode 240 搜索二维矩阵 II(二)
240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...
- 240. 搜索二维矩阵 II
二维数组搜索 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- LC 655. Print Binary Tree
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- 阶段3 3.SpringMVC·_06.异常处理及拦截器_5 SpringMVC拦截器之编写controller
先新建包,com.itcast.controller,然后把异常拦截的项目的UserController复制过来. 复制过来稍作修改 创建pages文件件,然后新建success.jsp页面 部署当前 ...
- 使用第三方UITableView+FDTemplateLayoutCell计算cell行高注意点
现在很方便的计算单元格的行高大部分都是使用的第三方框架UITableView+FDTemplateLayoutCell,不知道你在使用这个框架的时候有没有遇到和我一样的问题,比如: 在这样计算cell ...
- 第一个python-ui界面
首先是安装eric6简直是个灾难,先是找不到汉化版的eric6,好不容易找到了,一打开eric6的窗体就说designer.exe不存在,确实在PyQt5里没有,明明在PyQt5-tools里面有,最 ...
- Jquery(DOM和选择器)
O(∩_∩)O~~~,今天简单整理了一下最近所学的Jquery知识.下面就总结一下. 首先,对于Jquery我们需要简单了解下: 1.Jquery是开放源代码的JS库, 2.Jquery操作是函数式编 ...
- sshpass密码
使用sshpass sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path ...
- 从gopath到go mod的一次尝试
windows下的尝试: gomod初尝试下载官方包1.11(及其以上版本将会自动支持gomod) 默认GO111MODULE=auto(auto是指如果在gopath下不启用mod)go mod h ...
- CTF—攻防练习之HTTP—SQL注入(SSI注入)
主机:192.168.32.152 靶机:192.168.32.161 ssI是赋予html静态页面的动态效果,通过ssi执行命令,返回对应的结果,若在网站目录中发现了.stm .shtm .shtm ...
- UOJ#494K点最短路
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #d ...
- Oracle 看出表结构与属性、表空间设计
1.Oracle 查看表空间 SELECT b.comments as 注释, a.column_name as 列名, a.data_type || '(' || a.data_length || ...