Simple Java Solution using Binary Search


  • 0
    D
    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            int row=matrix.length,col=matrix[0].length;
            int l=0,r=row*col-1;
            while(l<=r){
                int mid=(r+l)/2;
                if(matrix[mid/col][mid%col]==target){
                    return true;
                }else if(matrix[mid/col][mid%col]<target){
                    l=mid+1;
                }else{
                    r=mid-1;
                }
            }
            return false;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.