Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This
2017-03-06 15:21
671 查看
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 in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
Given target =
Given target =
在这道题中,由于矩阵的行和列已经分别排序好了,所以可以利用分治算法,将矩阵分为行和列分开比较,先比较每一列最后一个数与查找目标的大小,
如果比目标数小,则比较下一列最后一行的数和目标数的大小;如果比目标数大,则比较该数同一列上一行的数,逐次向上,一旦找到目标数,即返回
true,否则返回false。
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
if(row == 0) return false;
int column = matrix[0].size();
int x = row - 1;
int y = 0;
while(x >= 0 && y < m)
{
if(matrix[y][x] == target) return true;
else if(matrix[y][x] > target)
{
x--;
}
else y++;
}
return false;
}
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ]
Given target =
5, return
true.
Given target =
20, return
false.
在这道题中,由于矩阵的行和列已经分别排序好了,所以可以利用分治算法,将矩阵分为行和列分开比较,先比较每一列最后一个数与查找目标的大小,
如果比目标数小,则比较下一列最后一行的数和目标数的大小;如果比目标数大,则比较该数同一列上一行的数,逐次向上,一旦找到目标数,即返回
true,否则返回false。
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
if(row == 0) return false;
int column = matrix[0].size();
int x = row - 1;
int y = 0;
while(x >= 0 && y < m)
{
if(matrix[y][x] == target) return true;
else if(matrix[y][x] > target)
{
x--;
}
else y++;
}
return false;
}
相关文章推荐
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
- Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
- CareerCup An in-place algorithm for String Transformation
- Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your datab
- FRESH algorithm for efficient route discovery in MANET
- Java for LeetCode 074 Search a 2D Matrix
- Spiral Matrix I & II && Search in Rotated Sorted Array I & II
- Write a program that gives count of common characters presented in an array of strings..(or array of
- [FIM]invalid-dn,Multiple values were specified for an attribute that can have only one value.
- System.AccessViolationException: Attempted to read or write protected memory This is often an indication that other memory is corrupt
- System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt".
- 关于accessviolationexception. Attempted to read or write protected memory. This is often an indication that other memory is corrup
- Message: Attempted to read or write protected memory. This is often an indication that other memory
- An example usage of an encryption algorithm (AES, in this case) is:
- DP27 2D矩阵中最大的矩形和 Maximum sum rectangle in a 2D matrix @geeksforgeeks
- Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your datab
- Search for a string in an infinite stream of input string.
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON