[Lintcode]28. Search a 2D Matrix

28.Search a 2D Matrix

Description

Description
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.
Have you met this question in a real interview?
Example
Example 1:
Input: [[5]],2
Output: false

Explanation: 
false if not included.

Example 2:
Input: [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
],3
Output: true

Explanation: 
return true if included.

Challenge
O(log(n) + log(m)) time

我的代码

class Solution:
    """
    @param matrix: matrix, a list of lists of integers
    @param target: An integer
    @return: a boolean, indicate whether matrix contains target
    """

    def searchMatrix(self, matrix, target):
        # write your code here
        if matrix == [] or matrix == [[]]:
            return False
        if matrix[0][0] > target:
            return False
        if matrix[-1][-1] < target:
            return False
        r, c = len(matrix), len(matrix[0])
        if matrix[-1][0] <= target:
            rp = r - 1
        else:
            rl = 0
            rr = r - 1
            rm = int((rl + rr) / 2)
            while rl < rm and rr > rm:
                if matrix[rm][0] == target:
                    return True
                else:
                    if matrix[rm][0] > target:
                        rr = rm
                        rm = int((rl + rr) / 2)
                    else:
                        rl = rm
                        rm = int((rl + rr) / 2)
            rp = rm
        cl = 0
        cr = c - 1
        cm = int((cl + cr) / 2)
        if matrix[rp][cl] == target or matrix[rp][cr] == target:
            return True
        while cl < cm and cm < cr:
            if matrix[rp][cm] == target:
                return True
            else:
                if matrix[rp][cm] > target:
                    cr = cm
                    cm = int((cl + cr) / 2)
                else:
                    cl = cm
                    cm = int((cl + cr) / 2)
        return False

别人的代码

class Solution:
    # @param matrix, a list of lists of integers
    # @param target, an integer
    # @return a boolean
    # 8:21
    def searchMatrix(self, matrix, target):
        if not matrix or target is None:
            return False

        rows, cols = len(matrix), len(matrix[0])
        low, high = 0, rows * cols - 1
        
        while low <= high:
            mid = (low + high) / 2
            num = matrix[mid / cols][mid % cols]

            if num == target:
                return True
            elif num < target:
                low = mid + 1
            else:
                high = mid - 1
        
        return False

思路:

我先对第一列做二分查找,再对找到的行做二分查找,但是我的特殊情况太多了,搞得代码非常不简洁非常不好看。
其实可以把它看作一个数组,则只需要一个二分查找,而且时间复杂度相同。

  • 时间复杂度: O(log(m)+log(n))
  • 出错:一开始的条件是cr<cl,这个条件很多情况下始终能满足,应该使用cr<rm &&cl>rm这种才能够达到预期的作用。
posted @ 2019-02-04 01:51  siriusli  阅读(128)  评论(0编辑  收藏  举报