Title Source: http://www.lintcode.com/zh-cn/problem/search-a-2d-matrix/
You can accept the following procedures:
1 classSolution {2 Public:3 /**4 * @param Matrix, a list of lists of integers5 * @param target, an integer6 * @return A Boolean, indicate whether matrix contains target7 */8 BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) {9 intn =matrix.size ();Ten if(n = =0) { One return false; A } - intm = matrix[0].size (); - intStart =0, end = n * M-1;//n rows M column the while(Start +1<end) { - intMID = start + (End-start)/2; - intRow = mid/m; - intCol = mid%m; + //counting starting from 0, rows and columns are also counted starting from 0, number of mid, mid/m row, mid%m column - if(Matrix[row][col] <target) { +Start =mid; A}Else { atEnd =mid; - } - } - if(matrix[start/m][start% m] = =target) { - return true; - } in if(matrix[end/m][end% m] = =target) { - return true; to } + return false; - } the};
[Easy] Search for two-dimensional matrices