1 """
2 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
3 Integers in each row are sorted in ascending from left to right.
4 Integers in each column are sorted in ascending from top to bottom.
5 Example:
6 Consider the following matrix:
7 [
8 [1, 4, 7, 11, 15],
9 [2, 5, 8, 12, 19],
10 [3, 6, 9, 16, 22],
11 [10, 13, 14, 17, 24],
12 [18, 21, 23, 26, 30]
13 ]
14 Given target = 5, return true.
15 Given target = 20, return false.
16 """
17 """
18 剑指offer原题,很经典的一道题
19 AC
20 """
21 class Solution:
22 def searchMatrix(self, matrix, target):
23 """
24 :type matrix: List[List[int]]
25 :type target: int
26 :rtype: bool
27 """
28 if not matrix:
29 return False
30 m = len(matrix)
31 n = len(matrix[0])
32 i, j = m-1, 0
33 while i >= 0 and j < n:
34 if target > matrix[i][j]:
35 j += 1
36 elif target < matrix[i][j]:
37 i -= 1
38 else:
39 return True
40 return False