# 总结排序（或部分排序）矩阵上的搜索问题

1762人阅读 评论(0)

1. LeetCode原题 Search a 2D Matrix：

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1,   3,  5,  7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]


Given target = 3, return true.

	public boolean Find(int[][] matrix, int elem) {
// binary search the row number
int low, high, mid = 0;
low = 0;
high = matrix.length - 1;

while (low <= high) {
mid = low + (high - low) / 2;
if (matrix[mid][0] == elem) {
return true;
} else if (matrix[mid][0] > elem) {
high = mid - 1;
} else {
low = mid + 1;
}
}

// make sure the row starts with a number less than the target element
if (matrix[mid][0] > elem) {
mid--;
}
int row = mid;

// binary search the column number
low = 0;
high = matrix[0].length - 1;
while (low < high) {
mid = low + (high - low) / 2;
if (matrix[row][mid] == elem) {
return true;
} else if (matrix[row][mid] > elem) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return false;
}

	public boolean find(int[][] matrix, int target) {
int numRows = matrix.length;
if (numRows == 0) return false;
int numCols = matrix[0].length;
int low = 0, high = numRows * numCols - 1;
int mid, col, row;

while (low <= high) {
mid = low + (high - low) / 2;
col = mid % numCols;
row = mid / numCols;

if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return false;
}

2. CareerCup原题：

Given a matrix in which each row and each column is sorted, write a method to find an element in it.

1. 思路1：(CareerCup给出的标准解法)

1）如果该元素等于目标元素，则找到目标；

2）如果该元素大于目标元素，则可以排除当前列，坐标往左移1个单位；

3）如果该元素小于目标元素，则可以排除当前行，坐标往下移1个单位。

	public boolean find(int[][] matrix, int elem) {
// start from the top right element
int row = 0, col = matrix[0].length - 1;
while (row < matrix.length && col >= 0) {
if (matrix[row][col] == elem) {
return true;
} else if (matrix[row][col] > elem) {
col--;
} else {
row++;
}
}

return false;
}

	// binary search in a given row, starting from the first column and ending
// with a given ending column index (inclusive)
private int binarySearchInRow(int[][] matrix, int row, int endCol,
int elem) {
int low = 0, high = endCol, mid = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (matrix[row][mid] == elem) {
return mid;
} else if (matrix[row][mid] > elem) {
high = mid - 1;
} else {
low = mid + 1;
}
}

// make sure the matrix[row][mid] value is greater than the target
// element
if (matrix[row][mid] < elem && mid < endCol) {
mid++;
}

// differentiate from the case when the target element is the first element in the row
if (mid == 0) {
return Integer.MAX_VALUE * -1;
}

return -1 * mid;
}

// binary search along diagonal: complexity O(M * logN)
public boolean find(int[][] matrix, int elem) {
// start from the top right element
int row = 0, col = matrix[0].length - 1;
// binary search via the diagonal
while (row < matrix.length && col >= 0) {
if (matrix[row][col] == elem) {
return true;
} else if (matrix[row][col] > elem) {
// binary search the current row ahead of the current column
int retCol = binarySearchInRow(matrix, row, col, elem);
if (retCol >= 0) {
return true;
} else {
col = retCol * -1;
if (col == Integer.MAX_VALUE)
return false;
row++;
}
} else {
row++;
}
}

return false;
}

Given a M * N Matrix. All rows are sorted, and all columns are sorted. Find the Kth smallest element of the matrix.

1.右边邻居；

2.下边邻居；

3.下边邻居的某些左侧元素。

1）每次弹出当前堆中的最小值，并将其右邻居和下邻居插入堆中，并标额外记右邻居和下邻居已经在堆中。如果之前某邻居已经被标记，则不再处理；（注意下邻居很可能之前是某元素的右邻居，要避免重复处理）

2）如果弹出的元素总数为K，则停止。

http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf

Greg N. Frederickson and Donald B. Johnson. Generalized Selection and Ranking: Sorted Matrices. SIAM J. Comput. 13, pp. 14-30. http://epubs.siam.org/sicomp/resource/1/smjcat/v13/i1/p14_s1?isAuthorized=no

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：199531次
• 积分：2620
• 等级：
• 排名：第14416名
• 原创：60篇
• 转载：19篇
• 译文：0篇
• 评论：48条
文章分类
阅读排行
评论排行
最新评论