当前位置:网站首页>Sword finger offer 04 Search in two-dimensional array
Sword finger offer 04 Search in two-dimensional array
2022-07-05 04:17:00 【xzystart】
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
class Solution {
public static boolean findNumberIn2DArray(int[][] matrix, int target) {
int cloumn= 0,row = matrix.length-1; // Position the subscript to the lower left corner of the matrix
// Because starting from the lower left corner or the upper right corner , That is, this array can be regarded as a binary search tree
while ((row>=0)&&cloumn<matrix[0].length){
// Traversing the binary search tree
if (matrix[row][cloumn] >target){
row--;
}else if (matrix[row][cloumn] < target){
cloumn++;
}else return true;
}
return false;
}
}
边栏推荐
- Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
- Possible stack order of stack order with length n
- 如何实现实时音视频聊天功能
- 10种寻址方式之间的区别
- 电源管理总线 (PMBus)
- Ctfshow web entry code audit
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- DFS and BFS concepts of trees and graphs
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- About the recent experience of writing questions
猜你喜欢
Threejs factory model 3DMAX model obj+mtl format, source file download
[thingsboard] how to replace the homepage logo
快手、抖音、视频号交战内容付费
The new project Galaxy token just announced by coinlist is gal
Rome chain analysis
【虚幻引擎UE】实现测绘三脚架展开动画制作
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Burpsuite grabs app packets
Threejs Internet of things, 3D visualization of farms (II)
如何实现实时音视频聊天功能
随机推荐
3. Package the bottom navigation tabbar
Machine learning decision tree
The new project Galaxy token just announced by coinlist is gal
网络安全-记录web漏洞修复
A solution to the problem that variables cannot change dynamically when debugging in keil5
[phantom engine UE] the difference between running and starting, and the analysis of common problems
25K 入职腾讯的那天,我特么哭了
Rome chain analysis
[brush questions] BFS topic selection
How to get the first few pieces of data of each group gracefully
Threejs rendering obj+mtl model source code, 3D factory model
基于TCP的移动端IM即时通讯开发仍然需要心跳保活
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
Study notes 7
[phantom engine UE] realize the animation production of mapping tripod deployment
Scheduling system of kubernetes cluster
open graph协议
A application wakes up B should be a fast method
Common features of ES6
Machine learning -- neural network