当前位置:网站首页>剑指Offer04. 二维数组中的查找【中等】
剑指Offer04. 二维数组中的查找【中等】
2022-07-03 11:50:00 【伍六琪】
剑指 Offer 04. 二维数组中的查找 【中等】
题目描述
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
现有矩阵 matrix 如下:
给定 target = 5,返回 true。
给定 target = 20,返回 false。
限制:
0 <= n <= 1000
0 <= m <= 1000
Java代码
暴力解法
简单粗暴两层遍历挨个找。
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for(int i = 0;i<matrix.length;i++){
for(int j = 0;j<matrix[i].length;j++){
if(matrix[i][j]==target){
return true;
}
}
}
return false;
}
}

官方解法
从二维数组的右上角开始查找。如果当前元素等于目标值,则返回 true。如果当前元素大于目标值,则移到左边一列。如果当前元素小于目标值,则移到下边一行。
注意:可以证明这种方法不会错过目标值。如果当前元素大于目标值,说明当前元素的下边的所有元素都一定大于目标值,因此往下查找不可能找到目标值,往左查找可能找到目标值。如果当前元素小于目标值,说明当前元素的左边的所有元素都一定小于目标值,因此往左查找不可能找到目标值,往下查找可能找到目标值。
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row=0,column=matrix[0].length-1;
while(row<matrix.length&&column>=0){
if(matrix[row][column]==target){
return true;
}else if(matrix[row][column]<target){
row++;
}else{
column--;
}
}
return false;
}
}

补充思路:
关键是要能够想到,从二维数组的右上角开始遍历。
- 因为左上和右下角分别是数组的最大值和最小值,所有有两个方向可以同时选择,无法确定唯一。
- 而左下和右上就可以,就拿右上角举例,它是行的最大值,列的最小值。如果它比目标值小,就可以排除这一行数据,列+1(因为它已经是这一行的最大值,比目标值还小,说明这一行数据都太小了,不可能有目标值。故列+1舍弃。)
还有另一种思路就是:
- 在右上角看。这个矩阵其实就像是一个Binary Search Tree二叉查找树。
边栏推荐
- Kubernetes three dozen probes and probe mode
- Flutter: about monitoring on flutter applications
- Flutter Widget : Flow
- Use of atomicinteger
- Optimize interface performance
- Summary of development issues
- 网上炒股开户安不安全?谁给回答一下
- Flutter Widget : KeyedSubtree
- repo Manifest Format
- Fundamentals of concurrent programming (III)
猜你喜欢

Shutter: add gradient stroke to font

Integer string int mutual conversion

4000字超详解指针

PHP export word method (one MHT)

2.8 overview of ViewModel knowledge

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

shardingSphere分库分表<3>

Colleagues wrote a responsibility chain model, with countless bugs

Itext7 uses iexternalsignature container for signature and signature verification

Download address and installation tutorial of vs2015
随机推荐
Kubernetes three dozen probes and probe mode
lambda与匿名内部类的区别
Pragma pack syntax and usage
Use of atomicinteger
The difference between lambda and anonymous inner class
error: expected reference but got (raw string)
PHP导出word方法(一phpword)
242. Effective letter heteronyms
PHP导出word方法(一mht)
wpa_ cli
Self made pop-up input box, input text, and click to complete the event.
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
Shutter: overview of shutter architecture (excerpt)
2020-11_ Technical experience set
Solve msvcp120d DLL and msvcr120d DLL missing
Unicode encoding table download
网络通讯之Socket-Tcp(一)
20. Valid brackets
Is BigDecimal safe to calculate the amount? Look at these five pits~~
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework