题解 | #二维数组中的查找#
二维数组中的查找
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
public class Solution {
public boolean Find(int target, int [][] array) {
int rows=array.length;
int cols=array[0].length;
int r=0;
int c=cols-1;
//对于一个数n,其左、上均比它小,其右、下均比它大
//从右上角开始找,小于在左边,大于它在下面
while(r<rows&&c>=0){//要限制c>=0,否则java.lang.ArrayIndexOutOfBoundsException: -1
if(target==array[r][c])return true;
else if(target<array[r][c])c--;
else r++;
}return false;
}
}
科大讯飞公司氛围 455人发布