在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
从右上角开始搜寻,大于目标值往左走,小于目标值往下走。需确保不越界。
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int rows, cols, temp, p, q;
rows = array.size();
cols = array[0].size();
p = 0, q = cols - 1;
while(p >= 0 && p <= (rows-1) && q >= 0 && q <= (cols-1))
{
temp = array[p][q];
if(temp > target)
{
q--;
continue;
}
else if(temp < target)
{
p++;
continue;
}
else
{
return true;
}
}
return false;
}
};