The title is described in a two-dimensional array in which 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. The first method, in each line of the two-point lookup, Nlogn. The second method, may also be the best, because it is already orderly, we choose the lower left or right corner, each time to determine the size, choose to move up or right, so every time there is greedy choice, but how to determine the complexity of the time? Pay special attention to the judgment of the boundary! Otherwise null pointer exception. Get the incoming array and get the subscript among other things to judge.
classSolution { Public BooleanFind (intTargetint[] Array) { if(Array.Length = = 0)return false; //start from the lower left corner intr = Array.length-1; intL = 0; while(true) { if(r<0 | | l>array[0].length-1)return false; if(Array[r][l] >target) {R--; }Else if(Array[r][l] <target) {L++; }Else { return true; } } }} Public classMain { Public Static voidMain (string[] args) {intA = 16; int[] Array = {}; Solution So=Newsolution (); System.out.println (So. Find (16, array)); }}
classSolution { Public BooleanFind (intTargetint[] Array) { for(inti = 0; i < Array.Length; i++) { intL = 0; if(array.length<=0)return false; if(array[0].length<=0)return false; intr = Array[0].length-1; while(l<=r) {intMid = (l+r)/2; if(target>Array[i][mid]) {L= Mid+1; } Else if(target<Array[i][mid]) {R= Mid-1; } Else return true; } } return false; }} Public classMain { Public Static voidMain (string[] args) {intA = 16; int[] Array = {{1,3,4},{3,16,1}}; Solution So=Newsolution (); System.out.println (So. Find (16, array)); }}
Sword Point Offer (i) find a two-dimensional array