当前位置:网站首页>剑指Offer刷题记录——Offer 04. 二维数组中的查找
剑指Offer刷题记录——Offer 04. 二维数组中的查找
2022-07-17 05:22:00 【什么时候点菜】
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
示例:
现有矩阵 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。
限制:
0 <= n <= 1000
0 <= m <= 1000
方法一:自己写的:
从数组右上角开始逐行检索,如果当前元素等于目标值,则返回 true。如果当前元素大于目标值,则移到左边一列。如果当前元素小于目标值,则移到下边一行。
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for (int i = 0;i < matrix.length;i++){
for (int j = matrix[i].length - 1;j >= 0;j--){
if (matrix[i][j] == target)
return true;
else if (matrix[i][j] > target)
continue;
else break;
}
}
return false;
}缺点很明显,每一行的检索都是从最右边开始的,有许多不必要的检索。另外复习了二维数组的创建:int[][] arr1 = new int[][]{ {5},{6}};
方法二:改进的:
由于给定的二维数组具备每行从左到右递增以及每列从上到下递增的特点,当访问到一个元素时,可以排除数组中的部分元素。
从二维数组的右上角开始查找。如果当前元素等于目标值,则返回 true。如果当前元素大于目标值,则移到左边一列。如果当前元素小于目标值,则移到下边一行。
可以证明这种方法不会错过目标值。如果当前元素大于目标值,说明当前元素的下边的所有元素都一定大于目标值,因此往下查找不可能找到目标值,往左查找可能找到目标值。如果当前元素小于目标值,说明当前元素的左边的所有元素都一定小于目标值,因此往左查找不可能找到目标值,往下查找可能找到目标值。
若数组为空,返回 false
初始化行下标为 0,列下标为二维数组的列数减 1
重复下列步骤,直到行下标或列下标超出边界
获得当前下标位置的元素 num
如果 num 和 target 相等,返回 true
如果 num 大于 target,列下标减 1
如果 num 小于 target,行下标加 1
循环体执行完毕仍未找到元素等于 target ,说明不存在这样的元素,返回 false`
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int rows = matrix.length, columns = matrix[0].length;
int row = 0, column = columns - 1;
while (row < rows && column >= 0) {
int num = matrix[row][column];
if (num == target) {
return true;
} else if (num > target) {
column--;
} else {
row++;
}
}
return false;
}
边栏推荐
- [automated testing] - robotframework practice (II) new test cases
- What happened to redraiment
- tail -f暂停方法
- 1.服务器是什么?
- 小迪网络安全笔记-信息收集-架构、搭建、waf(8)
- Slag learning road (2) pure white direction: win Server 2003 server building
- Application case of CS brand SD NAND in air quality inspection industry
- 配置树莓派3b+搭建个人网站
- Class is coming. Roll call is needed
- Personal information management system
猜你喜欢

Xiaodi network security - Notes (4)

Steam游戏服务器配置选择 IP

Tianyi cloud Hangzhou virtual machine (VPS) performance evaluation
![[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :](/img/dd/054af819c8bdca31bd135495386fb4.png)
[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :

高并发day04(ZAB协议,观察者,nc,AVRO,RPC)

Tcp/ip four layer model and related configurations of F5

我的世界 1.18.1 Forge版 开服教程,可装MOD,带面板

Commande awk du troisième épéiste - - interception

高并发day02(Concurrent包)

银河麒麟服务器系统搭建本地和局域网yum源
随机推荐
寄居蟹和海葵
linxu下调试微信调一跳(Fedora 27)
[automated testing] - robotframework practice (III) writing test cases
[CS Genesis] comparative analysis of advantages and disadvantages of SD NAND and raw NAND
Alibaba cloud Hangzhou arm ECS performance evaluation
Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
How to determine the electronic gear ratio of servo motor?
Minecraft基岩版BDS开服教程
Text three swordsman's awk command -- interception
Minecraft Paper 1.18.1 版开服教程,我的世界开服教程,MCSManager9面板使用教程
Technical specification for secure electronic signature and password gm/t 0031 | GB / T 38540 format OpenSSL package analysis
Personal information management system
正则表达式,生成器,迭代器
Xiaodi network security - note encryption coding algorithm (6)
网站被劫持了怎么办?
Hermit crab and anemone
传奇游戏架设教程
Performance evaluation and comparison of Huawei cloud Kunpeng arm ECs and x86 ECS
Total price contract, cost compensation contract, labor contract
快速理解重定向