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.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- golang mysql 如何设置最大连接数和最大空闲连接数
本文介绍golang 中连接MySQL时,如何设置最大连接数和最大空闲连接数. 关于最大连接数和最大空闲连接数,是定义在golang标准库中database/sql的. 文中例子连接MySQL用的SQ ...
- CSS操作表格的边框和表格的属性示例代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C代码输出日志
模板代码,在实际开发中可以使用: Android.mk文件增加(放到 include $(CLEAR_VARS)下面) LOCAL_LDLIBS += -llog C代码中增加 #include &l ...
- shell做成csv文件
echo a,b,c,d >aa.csv 其实就是用逗号做分隔符
- WPF 快速制作可拖拽的对象和窗体
引用类库: 1.Microsoft.Expression.Interactions 2.System.Windows.Interactivity <Window x:Class="Wp ...
- SpringBoot解决ajax跨域问题(转载)
一.第一种方式: 1.编写一个支持跨域请求的 Configuration import org.springframework.context.annotation.Configuration; im ...
- Java split(".") 和 split("\\.")
Java split(".") 和 split("\\.") 问题描述 使用 . 分解 IP 的各个段,并打印,如:192.168.10.123,分解为 192 ...
- IPv4正则表达式
apache common-httpclient-4.5.2.jar package org.apache.http.conn.util; public class InetAddressUtils ...
- java base64相关
文件转Base64: public static String imgToBase64(InputStream inStream) { byte[] data = null; try { //avai ...
- 关于Linux文本处理“三剑客”的一些小操作。
Linux文本处理“三剑客”,即grep.sed.awk,这是Linux中最核心 的3个命令. 一.首先做个简单的介绍: 1.awk:linux三剑客老大,过滤,输出内容,一门语言.NR代表行号. 2 ...