2014年2月14日星期五

LeetCode - Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,
Consider the following matrix:
[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix==null || matrix.length==0 || matrix[0].length==0) {
            return false;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        int start = 0;
        int end = rows - 1;
        int mid = (start+end)/2;
        while(start<=end) {
            mid = (start+end)/2;
            if(matrix[mid][0]==target) {
                return true;
            } else if(matrix[mid][0]<target && mid+1<rows && matrix[mid+1][0]>target) {
                break;
            } else if(matrix[mid][0]>target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        int r = mid;
        start = 0;
        end = columns - 1;
        while(start<=end) {
            mid = (start+end)/2;
            if(matrix[r][mid]==target) {
                return true;
            } else if(matrix[r][mid]<target) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return false;
    }
}

=============

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0 || target < matrix[0][0]
            || target > matrix[matrix.length - 1][matrix[0].length - 1]) {
            return false;
        }
        int s = 0;
        int e = matrix.length - 1;
        int rowId = 0;
        while (s <= e) {
            int m = (s + e) / 2;
            if (matrix[m][0] <= target && (m + 1 == matrix.length || matrix[m + 1][0] > target)) {
                rowId = m;
                break;
            } else if (matrix[m][0] > target) {
                e = m - 1;
            } else {
                s = m + 1;
            }
        }
        s = 0;
        e = matrix[rowId].length - 1;
        while (s <= e) {
            int m = (s + e) / 2;
            if (matrix[rowId][m] == target) {
                return true;
            } else if (matrix[rowId][m] > target) {
                e = m - 1;
            } else {
                s = m + 1;
            }
        }
        return false;
    }
}

=======

public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0) { return false; } int start = 0, rows = matrix.length, cols = matrix[0].length; int end = rows * cols - 1; while (start <= end) { int mid = (start + end) / 2; if (matrix[mid / cols][mid % cols] == target) { return true; } if (matrix[mid / cols][mid % cols] < target) { start = mid + 1; } else { end = mid - 1; } } return false; } }

没有评论:

发表评论