Interview questions Find in 32-D arrays leetcode--74 search a 2D Matrix
1 /*Java2 Write An efficient algorithm, searches for a value in a m x n matrix. This matrix has the following properties:3 4 integers in each row is sorted from the left to the right.5 The first integer of each row was greater than the last integer of the previous row.6 7 For example,8 9 consider the following matrix:Ten One [ A [1, 3, 5, 7], - [Ten, one, +,], - [ Max., the ] - - Given target = 3, return true. - the simplest way to do this is to iterate through rows and columns but with a high degree of time complexity. + */ - classSolution { + Public BooleanSearchmatrix (int[] Matrix,inttarget) { A BooleanFind =false; at intRows,cols; - if(Matrix = =NULL|| Matrix.length==0) { -rows = 0; -cols = 0; -}Else { -rows =matrix.length; incols = matrix[0].length; - } to + if(Matrix! =NULL&& rows > 0 && cols > 0) { - intRow = 0; the intCol = cols-1; * while(Row<rows && col>=0){ $ if(matrix[row][col]==target) {Panax NotoginsengFind=true; - Break; the } + A Else if(matrix[row][col]>target) { thecol--; +}Else{ -row++; $ } $ } - } - the returnfind; - }Wuyi Public Static voidMain (string[] args) { the int[] matrix1 = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50 } }; - Wu int[] Matrix = {}; - inttarget = 55; AboutSearcha2dmatrix cc =NewSearcha2dmatrix (); $ BooleanFind =Cc.searchmatrix (matrix, target); - System.out.println (find); - } -}View Code
"Sword Point offer" Java implementation