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 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- js闭包解决多个点击事件
<script> var severalObj=window.document.getElementsByName("button"); for(var i=0;i&l ...
- Struts ognl表达式语言几个符号
# 获取非根元素值 . 动态都建map集合 $ 配置文件取值 % 提供一个ognl表达式运行环境 <%@ page language="java" import=&q ...
- php获取文件名和后缀名
php获取文件名 1 function retrieve($url) 2 { 3 preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$url,$match); 4 re ...
- Python使用filetype精确判断文件类型
Python使用filetype精确判断文件类型 判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是 ...
- This application's application-identifier entitlement does not match that of the installed application. These values must match for an upgrade to be allowed.
真机运行测试的时候Xcode会报这样的错误: 原因: 你的手机上已经安装了此项目. 解决办法: 把你以前安装的卸掉, 或者把这个项目的 bunldID 改了,再次运行即可.
- JavaScript(1):Base/Tips
目录 输出 全局变量 字符串 类型及转换 变量提升 严格模式 表单验证 (1) 输出 <!DOCTYPE html> <html> <body> <p> ...
- [转] An In-Depth Look at the HBase Architecture - HBase架构深度剖析
[From] https://mapr.com/blog/in-depth-look-hbase-architecture/ In this blog post, I’ll give you an i ...
- 2019 1月 第三次java基础有感
毕业半年了,一直在游戏公司做游戏服务器开发,java语言. 工作中,写着写着代码,接触java多了,有时候就会发现自己的java基础会不够用.以前实习的时候也体会到一次,然后过了一遍基础.现在正式工作 ...
- (转)Dubbo + Zookeeper入门初探
一.搭建java和tomcat环境 二.搭建zookeeper 三.搭建dubbo监控中心 四.配置项目 4.1 服务提供方代码 4.2 服务使用方代码 五.测试 2018年2月15日,阿里巴巴的du ...