当前位置:网站首页>Finding a 2D Array
Finding a 2D Array
2022-07-30 00:17:00 【Ryuzaki Liuhe】
题目:
在一个 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.
分析:
从左下角开始查找,Subtract the number of rows if the current number is greater than the target value1,The number of columns is added if the current number is less than the target value1,until the target value is found.
代码:
public class FindNumberIn2DArray {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
//Remember not to forget this judgment on the array
if(matrix == null || matrix.length <= 0 || matrix[0].length <= 0){
return false;
}
int rows = matrix.length;
int cols = matrix[0].length;
//Determine the position of the lower left corner
int row = rows-1;
int col = 0;
while (row >= 0 && col <= cols - 1){
if (target > matrix[row][col]){
col++;
}else if(target < matrix[row][col]){
row--;
}else {
return true;
}
}
return false;
}
}

边栏推荐
- Docker install MySQL8.0
- rk-boot framework combat (1)
- what is a .pro file in qt
- 单片机开发之拓展并行I/O口
- vmtouch——Linux下的文件缓存管理神器
- Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
- YoloV5数据集自动划分训练集、验证集、测试集
- 重建二叉树
- Override and customize dependent native Bean methods
- Google Chrome (google) is set to translate Chinese, the translation option does not take effect or the translation option does not pop up
猜你喜欢

Worthington解离酶:中性蛋白酶(分散酶)详情解析

自媒体人如何打造出爆文?这3种类型的文章最容易爆

Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"

Based on TNEWS 'today's headline news in Chinese short text classification

Since the media how to write a short video title?Three hot style title, let your video gain more traffic

Worthington's tried and tested cell isolation system protocols

3 tips for using hot events to create press releases?A must-see for self-media people

自媒体短视频标题怎么写?3个爆款标题,让你的视频收获更多流量

指令集数据产品如何设计和实现报表协同系统——基于指令集物联网操作系统的工业协同制造项目开发实践

Douyin short video traffic acquisition strategy, mastering these will definitely be a hit
随机推荐
rk-boot framework combat (1)
『牛客|每日一题』走迷宫
"The lighthouse factory" of China path: smart roll out from point to surface
Weekly recommended short video: What is R&D efficiency?It can achieve anti "involution"?
vim相关介绍(三)
Worthington细胞分离技术丨基本原代细胞分离方法和材料
557. 反转字符串中的单词 III
Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
基于TNEWS‘ 今日头条中文新闻(短文本)分类
某团实习面经
BEVDetNet: Bird's Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi
Worthington经过使用测试的细胞分离系统方案
头条号自媒体运营:如何在今日头条涨500+粉丝?
第一范式、第二范式、第三范式
关于 byte 的范围
Worthington dissociating enzyme: detailed analysis of neutral protease (dispase)
循环神经网络(RNN)
Laravel 预防 SQL 注入
X64 mfc140u.dll文件缺失->应用程序无法正常启动(0xc000007b)解决方法
EA&UML日拱一卒-状态图::重画按钮状态图