Title 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.
Thinking of solving problems
Iterates from the upper-right element, deleting the entire row if it is smaller than the target, or deleting the entire column if it is larger than the target. Each execution deletes a row or column and executes up to 2n times.
Implementation code
classSolution { Public:BOOLFind ( vector<vector<int> > Array,intTarget) {intrend =Array. Size ();intCend =Array[0].size (); for(inti =0, j = cend-1; I < rend && J >=0;) {if(ArrayI [j] = = target) {return true; }Else if(ArrayI [j] < target) {i++; }Else{j--; } }return false; }};
Finding in a two-dimensional array