74. Search a 2D Matrix

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length==0||matrix[0].length==0)
            return false;
        int m=matrix.length;
        int n=matrix[0].length;
        int lo=0;
        int hi=m*n-1;
        while(lo<hi)
        {
            int mi=(lo+hi)/2;
            if(target<=matrix[mi/n][mi%n])
                hi=mi;
            else
                lo=mi+1;
        }
        return matrix[lo/n][lo%n]==target;
    }
}

 

posted @ 2017-09-26 11:43  Weiyu Wang  阅读(95)  评论(0编辑  收藏  举报