当前位置:网站首页>Search two-dimensional matrix [clever use of bisection + record solution different from inserting bisection]
Search two-dimensional matrix [clever use of bisection + record solution different from inserting bisection]
2022-06-25 01:37:00 【REN_ Linsen】
Record details that are different from inserting a dichotomy
Preface
An ordered array or something , It should be closely related to the dichotomy . But dichotomy has many details , It is mainly divided into two parts / Two point insertion search position . Today, I have reached this question , Requirements are different from binary insertion to find the position , It's looking for something better than target Just a small position , Not the last one .
One 、 Search for a two-dimensional matrix

Two 、 Record solutions that are different from inserting dichotomy
package everyday.medium;
// Search for a two-dimensional matrix
public class SearchMatrix {
/* target: Quickly search whether there is a target value in the matrix . Every row of the matrix has a characteristic , That's order , And the columns are ordered , But this order is not true . Think of a matrix as a bit matrix ( tile ), The answer is known . So the line and column can be divided twice . */
public boolean searchMatrix(int[][] matrix, int target) {
// bug1: Column before row , It should be listed first .
// Dichotomy target In the line .
int low = 0, high = matrix.length - 1;
while (low < high) {
// This is a place worth recording
// Different from inserting dichotomy , What we are looking for here is just like this large number of positions , Instead of the latter position .
// It's also very simple ,int mid = low + (high - low + 1 >>> 1); coordination else low = mid;
int mid = low + (high - low + 1 >>> 1);
int midVal = matrix[mid][0];
if (midVal > target) high = mid - 1;
else low = mid;
}
// look for target In which column .
int col = binarySearch(matrix[low], target);
return matrix[low][col] == target;
}
private int binarySearch(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
// Take it up , It is different from the usual ascending insertion , Here we need to find a small place just like ourselves .
int mid = low + (high - low + 1 >>> 1);
int midVal = nums[mid];
if (midVal > target) high = mid - 1;
else low = mid;
}
return low;
}
}
summary
1) Order and dichotomy are closely linked , Be connected in your mind .
2) Different from the solution of inserting dichotomy .
reference
边栏推荐
- Baidu voice synthesizes voice files and displays them on the website
- Properties of DOM
- Abnova丨BSG 单克隆抗体中英文说明
- Abnova丨CSV 磁珠中英文说明
- Abnova CSV magnetic beads description in Chinese and English
- PMP考试“临门一脚”如何踢得漂亮?
- JVM指令
- 天书夜读笔记——8.4 diskperf反汇编
- Deoxyribonuclease I instructions in Chinese and English
- 屡获大奖的界面控件开发包DevExpress v22.1官宣发布
猜你喜欢

Fan benefits, JVM manual (including PDF)

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

Assembly language (4) function transfer parameters

Boutique enterprise class powerbi application pipeline deployment

Bi-sql create

15. several methods of thread synchronization

实验5 8254定时/计数器应用实验【微机原理】【实验】

AUTOCAD——两种延伸方式

Deep learning LSTM model for stock analysis and prediction

Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations
随机推荐
AssertionError: CUDA unavailable, invalid device 0 requested
Redis basic commands and types
Unity C# 网络学习(六)——FTP(一)
Bi SQL constraints
Bi skill - judge 0 and null
Chinese and English instructions of Papain
实验5 8254定时/计数器应用实验【微机原理】【实验】
Matlab rounding
Ps5 connected to oppo K9 TV does not support 2160p/4k
Tencent cloud wecity solution
Unity C# 网络学习(六)——FTP(二)
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
梦想CAD云图与GIS结合演示
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
弹性蛋白酶中英文说明书
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
同一服务器两个端口不同的应用session覆盖解决方案
Tianshu night reading notes -- memory paging mechanism
RedisTemplate操作Redis,这一篇文章就够了(一)[通俗易懂]
PMP考试“临门一脚”如何踢得漂亮?