当前位置:网站首页>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
边栏推荐
- Numerical scheme simulation of forward stochastic differential equations with Markov Switching
- Excel Chinese character to pinyin "suggestions collection"
- 梦想CAD云图与GIS结合演示
- Cloud development technology summit · public welfare programming challenge [hot registration]!
- Assembly language (4) function transfer parameters
- TC对象结构和简称
- Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代
- ‘distutils‘ has no attribute ‘version
- poj3669 Meteor Shower(bfs预处理)
- 木瓜蛋白酶中英文说明书
猜你喜欢

新一代可级联的以太网远程I/O数据采集模块

Baidu voice synthesizes voice files and displays them on the website

Use redis' sorted set to make weekly hot Reviews

PMP考试“临门一脚”如何踢得漂亮?

Bi SQL alias

void* 指针

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

Basic knowledge of assembly language (2) -debug

Application session coverage solutions with different ports on the same server

JVM directive
随机推荐
Bi-sql - different join
高考之后,必然会出现以下四种情况:
Abnova CSV magnetic beads description in Chinese and English
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
Ideas and examples of divide and conquer
结合实操带你吃透Redis持久化
Deoxyribonuclease I instructions in Chinese and English
Cloud development technology summit · public welfare programming challenge [hot registration]!
Bi skill - judge 0 and null
Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
Poj3669 meteor shower (BFS pretreatment)
Bi-sql select into
Excel Chinese character to pinyin "suggestions collection"
JVM指令
Abnova BSG monoclonal antibody description in Chinese and English
Redis persistence
归并排序模板 & 理解
最长连续序列[扩散法+空间换时间]
新一代可级联的以太网远程I/O数据采集模块