题解 | #矩阵元素查找#
矩阵元素查找
http://www.nowcoder.com/practice/3afe6fabdb2c46ed98f06cfd9a20f2ce
class Solution {
public:
vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
// write code here
vector<int> A;
if(mat.size()<=0||mat[0].size()<=0)
return A;
int i=n-1,j=0;//从左下角开始查找x
while(i>=0&&j<m){
if(mat[i][j]==x){
A.push_back(i);
A.push_back(j);
return A;
}
else if(x>mat[i][j]){
j++;
}
else i--;
}
return A;
}
};
public:
vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
// write code here
vector<int> A;
if(mat.size()<=0||mat[0].size()<=0)
return A;
int i=n-1,j=0;//从左下角开始查找x
while(i>=0&&j<m){
if(mat[i][j]==x){
A.push_back(i);
A.push_back(j);
return A;
}
else if(x>mat[i][j]){
j++;
}
else i--;
}
return A;
}
};