Search a 2D Matrix II:
You know, Python, two weeks, maybe a little bit of a beginner, don't laugh at me.
Problem Solving Ideas:
First, if the array is a 1-d array, the IF in direct lookup can
When m x n-dimensional arrays:
Matrix for row and column to increase the order of the Matrix so the row and column using binary lookup, you can determine the value of the new matrix range, step by step, if there is a row binary or column binary will certainly traverse to, otherwise does not exist in the matrix
The first element of each row is the minimum value, and the last element in each column is the maximum value if the first element of this line is larger than target, then the line number is smaller than the row, and if the last element of this column is smaller than target, it must be in a column with a column number greater than this column. Re-delimit the scope of the narrowing matrix to query again, such as Find 15
"' Search a 2d matrix ii ' class solution: # @param {integer[][]} matrix # @param {integer} target # @return {boolean} # @author sallency 32448732 # @date 2015/07/27 def searchmatrix (Self, matrix, target): #matrix rows and cols rows = len (Matrix) #如果是二维数组 if (Type (matrix[0]) == list) : cols = len (Matrix[0]) if (Isinmatrix (matrix, rows - 1, 0,&nbSp;target)) : return true else : return false #如果是一位数组直接用遍历 else : if target in matrix : return True else : return False ' ' Isinmatrix is used to determine if the target is in the matrix because the matrix is an ordered matrix with binary to find out the line boundary containing the target first, and then to exclude the column boundaries from the new scope by gradually narrowing the range. If target exists in the matrix, it will be traversed when a new boundary is determined, otherwise the final range is0 The @param matrix lookup matrix principal @param row_border col_border The new boundary of the current scope query is not found @target Find the target ' Def isinmatrix ' (matrix, row_border, col_border, target): rows_list = [] #from 0 to row_border for i in range ( row_border + 1): #列标为cols_ Border rows_list.append (Matrix[i][col_ Border]) rows_l, rows_r = 0, row_border #binary search to find the row gt target first while (rows_l <= Rows_r) &NBSP;:&NBSP;&NBSP;&NBSP;&Nbsp; rows_avg = (Rows_l + rows_r) / 2 if (Rows_list[rows_avg] == target) : return true elif (Rows_list[rows_avg] > target) : #row_avg -1 is the value border of target # Overflow if (rows_avg - 1 < 0) : return false #new range row border if (rows_list[rows_avg - 1] < target) : row_border = rows_avg - 1 break else : rows_r = rows_avg - 1 else : &Nbsp; rows_l = rows_avg + 1 if (Rows_l > rows_r) : row_border = rows_l - 1 #the new matrix row range is from 0 to row_border #binary search to find the col gt target first cols_list = [] for j in range ( Col_border, len (Matrix[0]): cols_ List.append (Matrix[row_border][j]) cols_l, cols_r = col_border, lEn (matrix[0]) - 1 while (cols_l <= Cols_r) : cols_avg = ( Cols_l + cols_r) / 2 if (Matrix[row_border][cols_avg] == target) : return True elif (Matrix[row_border][cols_avg] > target) : #overflow if (cols_avg - 1 < 0) : return false #new range col border if (Matrix[row_border][cols_avg - 1] < target) : col_border = cols_avg break else : cols_r = cols_avg - 1 else : cols_l = cols_avg + 1 if (Cols_l > cols_r) : col_border = cols_l #the new matrix col range is from col_border to max_left #if overflow if (Row_border > len (matrix) - 1 or col_border > len (Matrix[0]) - 1) : return False else : return isinmatrix (Matrix, row_border, col _border, target)
Search a 2D Matrix II "original"