0ms Java Solution


  • 0
    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                return false;
            }
            int row = matrix.length;
            int col = matrix[0].length;
            int startY = 0;
            int endY = row - 1;
            while (startY < endY) {
                int mid = startY + (endY - startY) / 2;
                if (matrix[mid][col - 1] == target) {
                    return true;
                } else if  (matrix[mid][col - 1] < target) {
                    startY = mid + 1;
                } else {
                    endY = mid;
                }
            }
            int startX = 0;
            int endX = col - 1;
            while (startX < endX) {
                int mid = startX + (endX - startX) / 2;
                if (matrix[startY][mid] == target) {
                    return true;
                } else if  (matrix[startY][mid] < target) {
                    startX = mid + 1;
                } else {
                    endX = mid - 1;
                }
            }
            return matrix[startY][startX] == target;
        }
    }
    

Log in to reply
 

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