http://www.lintcode.com/zh-cn/problem/search-a-2d-matrix-ii/
class Solution { public: /** * @param matrix: A list of lists of integers * @param target: An integer you want to search in matrix * @return: An integer indicate the total occurrence of target in the given matrix */ int searchMatrix(vector<vector<int> > &matrix, int target) { // write your code here if(matrix.empty()) { return 0; } int rows = matrix.size(); int cols = matrix.at(0).size(); int sum = 0; if(rows>0 && cols >0) { int row = 0; int col = cols-1; while(row < rows && col >= 0) { if(matrix.at(row).at(col) > target) { col--; } else if(matrix.at(row).at(col) < target) { row++; } else if(matrix.at(row).at(col) == target) { sum++; if(row < rows) row++; } } } return sum; } };