当前位置:网站首页>[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)
边栏推荐
- Browser - clean up the cache of JS in the page
- Introduction to victoriametrics
- How is LinkedList added?
- Construction and maintenance of business websites [9]
- How to prevent your jar from being decompiled?
- China plastic bottle and container market trend report, technological innovation and market forecast
- 20220702-程序员如何构建知识体系?
- China plastic bottle market trend report, technological innovation and market forecast
- Baidu sued a company called "Ciba screen"
- [shutter] shutter layout component (physicalmodel component)
猜你喜欢

Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source

*C language final course design * -- address book management system (complete project + source code + detailed notes)

The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"

What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together

Five message formats of OSPF

发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
![[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)](/img/fa/5c1b6c16d9aabd13e9a4f7c7b9c7da.jpg)
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

Baidu sued a company called "Ciba screen"
随机推荐
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
Gbase8s database type
GEE:(二)对影像进行重采样
Construction and maintenance of business websites [8]
Basic IO interface technology - microcomputer Chapter 7 Notes
【剑指 Offer】57. 和为s的两个数字
SQL必需掌握的100个重要知识点:管理事务处理
Chargement de l'image pyqt après décodage et codage de l'image
Basic knowledge of tree and binary tree (detailed illustration)
2019 Nanchang (relive the classic)
China microporous membrane filtration market trend report, technological innovation and market forecast
Centos7 installation and configuration of redis database
图像基础概念与YUV/RGB深入理解
分享一下如何制作专业的手绘电子地图
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
暑期第一周总结
在beforeDestroy中销毁localStorage中的值无效
How to prevent your jar from being decompiled?