当前位置:网站首页>[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)
边栏推荐
- [C language] [sword finger offer article] - replace spaces
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- Construction and maintenance of business websites [4]
- [Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
- Oriental Aesthetics and software design
- Daily book - low code you must understand in the era of digital transformation
- Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
- Construction and maintenance of business websites [10]
- Three chess games
- GEE:(二)对影像进行重采样
猜你喜欢
GEE:(二)对影像进行重采样
Introduction to the principle of geographical detector
Gee: (II) resampling the image
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Infrastructure is code: a change is coming
Basic IO interface technology - microcomputer Chapter 7 Notes
地理探测器原理介绍
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
How to write a good program when a big book speaks every day?
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
随机推荐
情感计算与理解研究发展概述
关于测试用例
技术人创业:失败不是成功,但反思是
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
Une semaine de vie
MySQL learning record (1)
一周生活
Gbase 8s database basic syntax
[CV] Wu Enda machine learning course notes | Chapter 12
"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
Error in PIP installation WHL file: error: is not a supported wheel on this platform
Summary of the first week of summer vacation
如何防止你的 jar 被反编译?
Redis distributed lock failure, I can't help but want to burst
System (hierarchical) clustering method and SPSS implementation
攻防世界pwn题:Recho
Construction and maintenance of business website [1]
#include<>和#include“”的区别
Three chess games