📜  搜索行列排序矩阵 leetcode - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:14.132000             🧑  作者: Mango

代码示例1
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length == 0) return false;
        int rows = matrix.length;
        int cols = matrix[0].length;
        
        int left = 0;
        int right = rows*cols-1;
        
        while(left <= right){
            int mid = left + (right-left)/2;
            int midElement = matrix[mid/cols][mid%cols];
            if(midElement == target) return true;
            if(midElement < target) left = mid+1;
            else right = mid-1;
        }
        return false;
    }
}