title : In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.
For example: The following two-dimensional array is each row, and each column increments the sort. Returns True if the number 7 is found in this array, or False if the number 5 is found because the array does not contain that number.
If you now want to determine whether the number 7 is included in the two-dimensional matrix, the lookup steps are as follows:
(a) 9>7, next to the left of 9, (b) 8>7, next to the left side of 8 to find;
(c) 2<7, next on the lower side of 2, (d) 4<7, next on the lower side of 4 to find.
Then the code of the algorithm can be expressed as follows:
BOOLFind (int* * Matrix,intRowsintColumnsintNumber ) { BOOLFound=false; if(Matrix!=null && rows>0&& columns>0){ introw=0; intcolumn=columns-1; while(Row<rows && column>=0)){ if(matrix[row][column]==Number ) {Found=true; Break; }Else if(matrix[row][column]>Number ) { --column; }Else { ++Row; } } } returnfound;}
Finding a two-dimensional array