当前位置:网站首页>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 .
边栏推荐
- (构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
- 剑指Offer10- I. 斐波那契数列
- Summary of development issues
- error: expected reference but got (raw string)
- ES6 standard
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Dart: About zone
- 2.9 overview of databinding knowledge points
- TOGAF认证自学宝典V2.0
- Self made pop-up input box, input text, and click to complete the event.
猜你喜欢
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use

Unicode encoding table download

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

Flutter 退出登录二次确认怎么做才更优雅?

Integer int compare size

Summary of development issues

ES6新特性

Shutter: add gradient stroke to font

LeetCode 0556. Next bigger element III - end of step 4
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock
随机推荐
Socket TCP for network communication (I)
Self made pop-up input box, input text, and click to complete the event.
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
QT OpenGL rotate, pan, zoom
Atomic atomic operation
【嵌入式】---- 内存四区介绍
Is it OK to open an account for online stock speculation? Is the fund safe?
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Use of atomicinteger
Why can't my MySQL container start
Redis notes 01: Introduction
Flutter Widget : Flow
4000 word super detailed pointer
elastic_ L02_ install
OpenGL shader use
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
剑指Offer06. 从尾到头打印链表
Shutter: about inheritedwidget