剑指offer-面试题3-二维数组中的查找

算法过程:

1.选取数组中右上角的数字

2.如果该数字小于target,则删除这个数字所在的行row++

3.如果该数字大于target,则删除这个数字所在的列column--

public class Find {
        
        public static boolean findNum(int[][] matrix,int target) {
                
                int num_row=matrix.length;
                int num_column = matrix[0].length;
                
                int row = 0;
                int column = num_column-1;
                
                while(row<num_row && column>=0) {
                        
                        if(matrix[row][column]==target) {
                                return true;
                        }else if(matrix[row][column]<target) {
                                ++row;
                        }else {
                                --column;
                        }
                         
                }
                
                return false;
                
        }
        
}

猜你喜欢

转载自blog.csdn.net/weixin_41993767/article/details/84205344