当前位置:网站首页>[leetcode] sword finger offer 04 Search in two-dimensional array
[leetcode] sword finger offer 04 Search in two-dimensional array
2022-07-02 21:52:00 【Chinese fir sauce_】
subject :
The finger of the sword Offer 04. Search in a two-dimensional array
In a n * m In a two-dimensional array , Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete an efficient function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Example :
The existing matrix matrix as follows :
[
[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]
]
Given target = 5, return true.
Given target = 20, return false.
Limit :
0 <= n <= 1000
0 <= m <= 1000
Linear search :
Start at the top right .
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0) return false;
int n = matrix.length - 1;
int m = matrix[0].length - 1;
for(int i = 0,j = m ; i <= n && j >= 0;){
if(target < matrix[i][j]) j--;
else if (target > matrix[i][j]) i++;
else {
return true;
}
}
return false;
}
}
Time complexity :O(m+n)
Spatial complexity :O(1)
边栏推荐
- pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
- Construction and maintenance of business websites [8]
- 2019 Nanchang (relive the classic)
- 100 important knowledge points that SQL must master: management transaction processing
- Une semaine de vie
- SQL必需掌握的100个重要知识点:管理事务处理
- MySQL learning record (2)
- Centos7 installation and configuration of redis database
- Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
- The failure rate is as high as 80%. What should we do about digital transformation?
猜你喜欢

Daily book -- analyze the pain points of software automation from simple to deep

Evolution of messaging and streaming systems under the native tide of open source cloud

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba

CVPR论文解读 | 弱监督的高保真服饰模特生成

Error in PIP installation WHL file: error: is not a supported wheel on this platform

Cardinality sorting (detailed illustration)

Redis distributed lock failure, I can't help but want to burst

MySQL learning record (6)

VictoriaMetrics 简介

基本IO接口技术——微机第七章笔记
随机推荐
Plastic granule Industry Research Report - market status analysis and development prospect forecast
*C language final course design * -- address book management system (complete project + source code + detailed notes)
CVPR论文解读 | 弱监督的高保真服饰模特生成
Daily book CSO advanced road first exposed
Evolution of messaging and streaming systems under the native tide of open source cloud
100 important knowledge points that SQL must master: using cursors
[shutter] shutter page Jump (route | navigator | page close)
Analysis of neural network
Basic IO interface technology - microcomputer Chapter 7 Notes
How to test the process of restoring backup files?
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Blue Bridge Cup Eliminate last one (bit operation, code completion)
Five message formats of OSPF
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
VictoriaMetrics 简介
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
Construction and maintenance of business websites [6]
Free open source web version of xshell [congratulations on a happy new year]