Skip to content

Latest commit

 

History

History
160 lines (120 loc) · 3.89 KB

面试题 10.09 Sorted Matrix Search LCCI.md

File metadata and controls

160 lines (120 loc) · 3.89 KB

Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.

Examples:

Given matrix:
[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

Solution

Brute-Force

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        for row in matrix:
            if target in row:
                return True
        return False

执行用时:32 ms, 在所有 Python3 提交中击败了99.69%的用户

内存消耗:19.5 MB, 在所有 Python3 提交中击败了25.77%的用户

Matrix-Cut

c++

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty() || matrix[0].empty()) return false;

        int row_upper_bound = matrix.size(), col_upper_bound = matrix[0].size();
        
        for (int i = 0; i < matrix.size(); i++)
            if (matrix[i][0] == target) return true;
            else if (matrix[i][0] > target) {
                row_upper_bound = i;
                break;
            }

        for (int i = 0; i < matrix[0].size(); i++)
            if (matrix[0][i] == target) return true;
            else if (matrix[0][i] > target) {
                col_upper_bound = i;
                break;
            }

        for (int i = 1; i < row_upper_bound; i++)
            for (int j = 1; j < col_upper_bound; j++)
                if (matrix[i][j] == target) return true;
        return false;
    }
};

超时

python

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return False

        row_index, col_index = len(matrix), len(matrix[0])

        for index, item in enumerate(matrix[0]):
            if item > target:
                col_index = index
                break
            elif item == target:
                return True
        
        for index, row in enumerate(matrix):
            if row[0] > target:
                row_index = index
                break
            elif row[0] == target:
                return True

        for i in range(1, row_index):
            row = matrix[i][1: col_index]
            if target in row: return True
        return False

执行用时:36 ms, 在所有 Python3 提交中击败了96.93%的用户

内存消耗:19.4 MB, 在所有 Python3 提交中击败了35.89%的用户

Double Pointer

初始点设置在右上角或左下角均可

c++

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty()) return false;

        int row = matrix.size(), col = matrix[0].size();
        int i = 0, j = col - 1;
        while (i < row && j >= 0){
            if (matrix[i][j] < target) i++;
            else if (matrix[i][j] > target) j--;
            else return true;
        }
        return false;
    }
};

执行用时:64 ms, 在所有 C++ 提交中击败了98.62%的用户

内存消耗:10.5 MB, 在所有 C++ 提交中击败了28.62%的用户

python

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        if not len(matrix) or not len(matrix[0]): #特判一下矩阵为空的情况
            return False
        
        row = 0
        col = len(matrix[0]) - 1
        
        while row != len(matrix) and col != -1:
            if matrix[row][col] > target:
                col -= 1
            elif matrix[row][col] < target:
                row += 1   
            else:
                return True
        return False

执行用时:48 ms, 在所有 Python3 提交中击败了66.06%的用户

内存消耗:18.2 MB, 在所有 Python3 提交中击败了20.33%的用户