当前位置:网站首页>Sword finger offer04 Search in two-dimensional array [medium]
Sword finger offer04 Search in two-dimensional array [medium]
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer 04. Search in a two-dimensional array 【 secondary 】
Title Description
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 :
Given target = 5, return true.
Given target = 20, return false.
Limit :
0 <= n <= 1000
0 <= m <= 1000
Java Code
Violence solution
Simple and rough, two-level traversal, one by one .
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
for(int i = 0;i<matrix.length;i++){
for(int j = 0;j<matrix[i].length;j++){
if(matrix[i][j]==target){
return true;
}
}
}
return false;
}
}
Official solution
Start from the upper right corner of the two-dimensional array . If the current element is equal to the target value , Then return to true. If the current element is larger than the target value , Move to the left column . If the current element is less than the target value , Move to the next line .
Be careful : It can be proved that this method will not miss the target value . If the current element is larger than the target value , It indicates that all elements below the current element must be greater than the target value , Therefore, it is impossible to find the target value by looking down , Looking to the left may find the target value . If the current element is less than the target value , It indicates that all elements on the left of the current element must be less than the target value , Therefore, it is impossible to find the target value by looking to the left , Look down and you may find the target value .
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row=0,column=matrix[0].length-1;
while(row<matrix.length&&column>=0){
if(matrix[row][column]==target){
return true;
}else if(matrix[row][column]<target){
row++;
}else{
column--;
}
}
return false;
}
}
Add ideas :
The key is to be able to think , Traverse from the upper right corner of the two-dimensional array .
- Because the upper left and lower right corners are the maximum and minimum values of the array respectively , All have two directions to choose at the same time , Unable to determine the unique .
- And the lower left and upper right can , Take the upper right corner for example , It is the maximum value of the line , Column minimum . If it is smaller than the target , You can exclude this line of data , Column +1( Because it is already the maximum value of this line , Smaller than the target value , It means that this line of data is too small , There can be no target value . So listed +1 Abandon .)
Another idea is :
- Look in the upper right corner . This matrix is actually like a Binary Search Tree Binary search tree .
边栏推荐
- PHP导出word方法(一mht)
- 写一个简单的nodejs脚本
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- Flutter: about monitoring on flutter applications
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- The difference between lambda and anonymous inner class
- (构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
- OpenGL draws colored triangles
- Shardingsphere sub database and sub table < 3 >
- Self made pop-up input box, input text, and click to complete the event.
猜你喜欢
Shardingsphere sub database and sub table < 3 >
Php Export word method (One MHT)
1-2 project technology selection and structure
QT OpenGL texture map
4000字超详解指针
C language improvement article (wchar_t) character type
OpenGL draws colored triangles
Socket TCP for network communication (I)
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
PHP export word method (phpword)
随机推荐
(数据库提权——Redis)Redis未授权访问漏洞总结
雲計算未來 — 雲原生
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
Flutter 退出登录二次确认怎么做才更优雅?
Talk about the state management mechanism in Flink framework
在网上炒股开户可以吗?资金安全吗?
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
2.7 overview of livedata knowledge points
SLF4J 日志门面
(构造笔记)GRASP学习心得
Pragma pack syntax and usage
Dart: about Libraries
Wechat applet - basic content
laravel 时区问题timezone
剑指Offer10- I. 斐波那契数列
145. Post order traversal of binary tree
1-2 project technology selection and structure
How to convert a numeric string to an integer
temp
Slf4j log facade