The original title link is here: https://leetcode.com/problems/search-a-2d-matrix-ii/
The search a 2D matrix is very similar to the title, but the content is very different.
The point is that each row of each column is ascending, starting at the top right corner x = matrix[i][j], i = 0, j = matrix[0].length-1, and with X and target, if equal, returns True.
If it is less than target, the description is definitely not in this line, so i++. If it is greater than target, the description must not be in this column so J--.
If the walk to the boundary has not returned stating that there is no target, return false.
The initial amount of note:j is J = n-1, note minus one.
AC Java:
1 Public classSolution {2 Public BooleanSearchmatrix (int[] Matrix,inttarget) {3 if(Matrix = =NULL|| Matrix.length = = 0 | | Matrix[0].length = = 0){4 return false;5 }6 intm =matrix.length;7 intn = matrix[0].length;8 inti = 0;9 intj = n-1;//ErrorTen while(I<m && j>=0){ One intx =Matrix[i][j]; A if(x = =target) { - return true; -}Else if(X <target) { thei++; -}Else{ -j--; - } + } - return false; + } A}
Leetcode Search a 2D Matrix II