class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
}
};
240. 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n
矩阵 matrix
中的一个目标值 target
。该矩阵具有以下特性:
示例 1:
输入: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]], target = 5 输出:true
示例 2:
输入: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]], target = 20 输出:false
提示:
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-109 <= matrix[i][j] <= 109
-109 <= target <= 109
相似题目
原站题解
python3 解法, 执行用时: 176 ms, 内存消耗: 21.2 MB, 提交时间: 2022-11-13 13:47:23
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: idx = bisect.bisect_left(row, target) if idx < len(row) and row[idx] == target: return True return False
python3 解法, 执行用时: 180 ms, 内存消耗: 21.3 MB, 提交时间: 2022-11-13 13:44:45
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: for element in row: if element == target: return True return False