給你一個滿足下述兩條屬性的?
m x n
?整數(shù)矩陣:
- 每行中的整數(shù)從左到右按非遞減順序排列。
- 每行的第一個整數(shù)大于前一行的最后一個整數(shù)。
給你一個整數(shù)?
target
?,如果?target
?在矩陣中,返回?true
?;否則,返回?false
?。
?文章來源:http://www.zghlxwxcb.cn/news/detail-672876.html
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
//從左下角還是找
int m = matrix.length, n = matrix[0].length;
int i = m - 1,j = 0;
while(i >= 0 && j < n){
if(matrix[i][j] == target){
return true;
}
else if(matrix[i][j] < target){
j++;
}
else{
i--;
}
}
return false;
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-672876.html
到了這里,關于Leetcode74. 搜索二維矩陣的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!