思路:
1 | 2 | 3 |
2 | 5 | 7 |
3 | 9 | 10 |
从左下角进行遍历,当target小于当前元素时,往右走,因为当前列上面的元素一定小于target;当terget大于当前元素时,忘上走,因为当前行右边所有元素一定大于target;当等于当前元素时,则查找成功;若遍历完没有找到,则失败。
代码:
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int row = array.size();
int col = array[0].size();
for(int i=row-1,j=0;i>=0&&j<col;){
if(array[i][j]==target) return true;
else if(array[i][j]<target){
j++;
continue;
}
else {
i--;
continue;
}
}
return false;
}
};