Topic:
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.
Ideas:
Given the regularity of the array, select the upper right-hand corner of the array lookup range,
Returns true if the number that is found is equal;
If it is larger than the number found, the column containing the number is excluded from the lookup range;
If it is smaller than the number found, the number is removed from the lookup range.
Code:
classSolution { Public: BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) { intRows=matrix.size (); intcols=matrix[0].size (); intR=0, c=cols-1; while(R<rows && c>=0){ if(target==Matrix[r][c])return true; if(target<Matrix[r][c]) C--; ElseR++; } return false; }};
Question 3: Finding in a two-dimensional array