Java O(logn) Solution 0ms


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

Log in to reply
 

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