跳到主要内容

240. 搜索二维矩阵 II

链接: 240. 搜索二维矩阵 II

斜着看, 是二叉搜索树!

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int n = matrix.size(), m = matrix[0].size();
int i = n - 1, j = 0;
while (i >= 0 && j < m) {
if (matrix[i][j] < target) {
++j;
} else if (matrix[i][j] > target) {
--i;
} else {
return true;
}
}
return false;
}
};
C++
请作者喝奶茶:
Alipay IconQR Code
Alipay IconQR Code
本文遵循 CC CC 4.0 BY-SA 版权协议, 转载请标明出处
Loading Comments...