题解 | #二维数组中的查找#
二维数组中的查找
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
因为题目中的二维数组 是有单调性的,所以我们可以从 右上角 的那个元素入手,如果 target 比右上角的元素大,则说明 target 不在该行,如果target比右上角元素小,则说明target不在该列,就可以实现要么排除掉 一行,要么排除掉一列
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int n=array.size();
int m=array[0].size();
int i=0,j=m-1;
while(i<n&&j>=0)
{
if(array[i][j]<target)
{
i++;
}
else if(array[i][j]>target)
{
j--;
}
else
{
return true;
}
}
return false;
}
};
#剑指offer#
