Problem Description: 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.
- First, select the number in the upper-right corner of the array. If the number is equal to the number you are looking for, the lookup process ends, and if the number is greater than the number you are looking for, the column containing the number is excluded, and if the number is less than the number you are looking for, the row containing the number is excluded. That is, if the number you are looking for is not in the upper-right corner of the array, each time you exclude a row or column from the array's look-up, each step narrows the search until you find the number you are looking for, or the lookup range is empty.
bool Find(int *matrix, int rows, int cols, int number){ bool found = false; if(matrix != nullptr && rows>0 && cols>0){ int row = 0; int col = cols-1; while(row<rows && col>=0){ if(matrix[row*cols+col] == number){ found = true; break; } else if(matrix[row*cols+col] < number) ++row; else --col; } } return found;}
Find in 3-two-D arrays