Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/iov3Rain/article/details/90242952
Title Description
There is a NxM integer matrix, the matrix of rows and columns are ordered from small to large. Please design an efficient search algorithm, find the location of the matrix elements of x.
Given a matrix MAT int ordered, while a given size of the matrix and n and m elements need to find x, return to a binary array, representing the elements of the row and column numbers (both from scratch). Ensure mutually different elements.
Test sample:
[[1,2,3],[4,5,6]],2,3,6
Returns: [1,2]
class Finder {
public:
vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
// write code here
vector<int> res;
int row = 0;
int col = m - 1;
while(row < n && col >= 0)
{
if(mat[row][col] > x)
{
--col;
}
else if(mat[row][col] < x)
{
++row;
}
else
{
res.push_back(row);
res.push_back(col);
return res;
}
}
return res;
}
};