LintCode--28.搜索二维矩阵

链接:搜索二维矩阵
解题思路:矩阵按行遍历,从右上角到左下角

public boolean searchMatrix(int[][] matrix, int target) {
        // write your code here

        if (matrix.length < 1) {
            return false;
        }
        int clum = matrix[0].length;
        int row = matrix.length;
        if (clum < 1 || row < 1) {
            return false;
        }

        int clumIndex = clum - 1;
        int rowIndex = 0;
        while (rowIndex < row && clumIndex >= 0) {
            int element = matrix[rowIndex][clumIndex];
            System.out.println(element);
            if (element == target) {
                return true;
            } else if (element < target) {
                rowIndex++;
            } else {
                clumIndex--;
            }

        }
        return false;

    }

你可能感兴趣的:(从零开始写算法,lintcede,二维矩阵搜索)