当前位置:网站首页>剑指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二叉查找树。
边栏推荐
- 手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
- 2.9 overview of databinding knowledge points
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- 网上炒股开户安不安全?谁给回答一下
- Lambda expression
- Colleagues wrote a responsibility chain model, with countless bugs
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- Laravel time zone timezone
- Integer int compare size
- New features of ES6
猜你喜欢

ES6新特性

使用BLoC 构建 Flutter的页面实例
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

The future of cloud computing cloud native

PHP導出word方法(一mht)

(construction notes) ADT and OOP

Quantitative calculation research

Symlink(): solution to protocol error in PHP artisan storage:link on win10

Colleagues wrote a responsibility chain model, with countless bugs

Prompt unread messages and quantity before opening chat group
随机推荐
102. Sequence traversal of binary tree
2.7 overview of livedata knowledge points
[download attached] password acquisition tool lazagne installation and use
AOSP ~ NTP (Network Time Protocol)
[learning notes] DP status and transfer
023(【模板】最小生成树)(最小生成树)
[official MySQL document] deadlock
[MySQL special] read lock and write lock
temp
02_ Lock the code, and don't let the "lock" become a worry
2.6 preliminary cognition of synergetic couroutines
regular expression
4000 word super detailed pointer
实现验证码验证
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Shutter: about inheritedwidget
Fluent: Engine Architecture
Systemverilog-- OOP--对象的拷贝
[combinatorics] permutation and combination (example of permutation and combination)