当前位置:网站首页>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
边栏推荐
- 中金证券靠谱吗?开证券账户安全吗?
- 论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
- Abnova BSG monoclonal antibody description in Chinese and English
- 结合实操带你吃透Redis持久化
- "One good programmer is worth five ordinary programmers!"
- 明日考试 最后一天如何备考?二造考点攻略全整理
- ‘distutils‘ has no attribute ‘version
- Texture enhancement
- Bi SQL drop & alter
- Basic use of transformers Library
猜你喜欢
Bi SQL drop & alter
"One good programmer is worth five ordinary programmers!"
Baidu voice synthesizes voice files and displays them on the website
Bi skill - judge 0 and null
Bi-sql Union
15. several methods of thread synchronization
Tianshu night reading notes -- memory paging mechanism
leetcode:2104. Subarray range and
JVM指令
Ps5 connected to oppo K9 TV does not support 2160p/4k
随机推荐
纹理增强
Tencent cloud wecity Hello 2022!
mpls 笔记 part 1
lnmp环境安装ffmpeg,并在Yii2中使用
excel 汉字转拼音「建议收藏」
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
Cobalt strike installation tutorial
天书夜读笔记——反汇编引擎xde32
The innovation consortium of Haihe laboratory established gbase and became one of the first member units of the innovation Consortium (Xinchuang)
高考之后,必然会出现以下四种情况:
15. several methods of thread synchronization
现状分析:“一云多芯”如何推动信创项目快速部署
Using macro code to generate handwriting automatically in word or WPS
Basic knowledge of assembly language (2) -debug
胰蛋白酶中英文说明书
Properties of DOM
第04天-文件IO
梦想CAD云图与GIS结合演示
poj3669 Meteor Shower(bfs预处理)
搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]