074 Search a 2D Matrix

一开始的想法是先确定行,在确定是否在这一行中 方法如下

class Solution:
    # @param {integer[][]} matrix
    # @param {integer} target
    # @return {boolean}
    def searchMatrix(self, matrix, target):
        m = len(matrix)
        if m == 0:
            return False
        if m == 1:
            return self.help(matrix[0], target)
        a = matrix[m/2][0]
        if a == target:
            return True
        elif a < target:
            return self.searchMatrix(matrix[m/2:], target)
        else:
            return self.searchMatrix(matrix[:m/2], target)

    def help(self, l, target):
        if l[0] > target or l[-1] < target:
            return False
        left = 0
        right = len(l)-1
        while left <= right:
            mid = (left + right) / 2
            if l[mid] == target:
                return True
            elif l[mid] > target:
                right = mid - 1
            else:
                left = mid + 1
        return False

后来想想本来就是二分,虽然有不同的行和列, 但其实可以看做一维的下标,方法如下。 与以上方法运时间差不多,因此并没有太大优点 最终复杂度都是 O(m+n)

class Solution:
    # @param {integer[][]} matrix
    # @param {integer} target
    # @return {boolean}
    def searchMatrix(self, matrix, target):
        m = len(matrix)
        if m == 0:
            return False
        n = len(matrix[0])
        left, right = 0, m*n-1
        while left <= right:
            mid = (left + right)/2
            a = matrix[mid/n][mid%n]
            if a == target:
                return True
            elif a < target:
                left = mid + 1
            else:
                right = mid - 1
        return False
时间: 2024-10-10 23:03:04

074 Search a 2D Matrix的相关文章

Leetcode 074 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 ro

Java for LeetCode 074 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 ro

LeetCode: Search a 2D Matrix [074]

[题目] 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 previo

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II 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 in ascend

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II 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 in ascending from left to right. Integers in each column are sorted in ascendin

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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 ro

[leedcode 240] Search a 2D Matrix II

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 in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

leetcode_74题——Search a 2D Matrix(数组查找)

Search a 2D Matrix Total Accepted: 40009 Total Submissions: 127082My Submissions Question Solution 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 fr

LeetCode: Search a 2D Matrix 解题报告

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