版权声明:只要梦想一天,只要梦想存在一天,就可以改变自己的处境。 https://blog.csdn.net/dongyanwen6036/article/details/86515654
74. 搜索二维矩阵/二维数组的查找
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty())return false;
//二分查找从左下角或右上角
int m = matrix.size()-1, n = matrix[0].size()-1;
int i = 0, j = n;
while (i <= m&&j >= 0)
{
if (target == matrix[i][j])return true;
else if (target < matrix[i][j])j--;
else i++;
}
return false;
}
};
240. 搜索二维矩阵 II
示例:
现有矩阵 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]
]
首先用上面的算法是可以通过的。