传送门:http://www.nowcoder.com/courses/1/2/18
题目描述
现在有一个行和列都排好序的矩阵,请设计一个高效算法,快速查找矩阵中是否含有值x。
给定一个int矩阵mat,同时给定矩阵大小nxm及待查找的数x,请返回一个bool值,代表矩阵中是否存在x。所有矩阵中数字及x均为int范围内整数。保证n和m均小于等于1000。
测试样例:
[[1,2,3],[4,5,6],[7,8,9]],3,3,10
返回:false时间复杂度可以做到O(M+N),额外空间复杂度为O(1);
AC代码
class Finder {
public:
bool findX(vector<vector<int> > mat, int n, int m, int x) {
int curRow=0,curCol=m-1;
while (true) {
if(mat[curRow][curCol]==x)
return true;
else{
if(x>mat[curRow][curCol])
curRow++;
else
curCol--;
if(curRow>=n||curCol<0)
return false;
}
}
}
};