题目地址:https://leetcode.com/problems/search-a-2d-matrix/description/
题目描述
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
1、 Integersineachrowaresortedinascendingfromlefttoright.;
2、 Integersineachcolumnaresortedinascendingfromtoptobottom.;
Example:
Consider the following 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.
题目大意
怪我理解能力太差?谁告诉我和74. Search a 2D Matrixopen in new window的区别?
给出了有规则的二维矩阵,规则是从左向右依次递增,从上向下依次递增。进行查找。
解题方法
和74. Search a 2D Matrixopen in new window完全一样的代码就A了。。我都蒙的。
下面是解释。
这个题在剑指offer的38-40页有详细解释。方法是从右上角向左下角进行遍历,根据比较的大小决定向下还是向左查找。
剑指offer的解释是我们从矩阵的左下角或者右上角开始遍历,这样知道了比较的结果是大还是小,就知道了对应的前进方向。
Python代码:
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix or not matrix[0]:
return False
rows = len(matrix)
cols = len(matrix[0])
row, col = 0, cols - 1
while True:
if row < rows and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
else:
col -= 1
else:
return False
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
C++代码如下:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == 0 || matrix[0].size() == 0) return false;
const int M = matrix.size(), N = matrix[0].size();
int i = M - 1, j = 0;
while (i >= 0 && j < N) {
if (matrix[i][j] == target) {
return true;
} else if (matrix[i][j] < target) {
++j;
} else {
--i;
}
}
return false;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
方法二:
暴力解法遍历,这也能过。。我实在看不懂leetcode了。。
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
return any(target in row for row in matrix)
1 2 3 4 5 6 7 8
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发