|
1 |
| -# [10.09. Sorted Matrix Search](https://leetcode-cn.com/problems/sorted-matrix-search-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>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.</p> |
5 |
| -
|
6 |
| -<p><strong>Example:</strong></p> |
7 |
| -
|
8 |
| -<p>Given matrix:</p> |
9 |
| -
|
10 |
| -<pre> |
11 |
| -[ |
12 |
| - [1, 4, 7, 11, 15], |
13 |
| - [2, 5, 8, 12, 19], |
14 |
| - [3, 6, 9, 16, 22], |
15 |
| - [10, 13, 14, 17, 24], |
16 |
| - [18, 21, 23, 26, 30] |
17 |
| -] |
18 |
| -</pre> |
19 |
| -
|
20 |
| -<p>Given target = 5, return <code>true.</code></p> |
21 |
| -
|
22 |
| -<p>Given target = 20, return <code>false.</code></p> |
23 |
| - |
24 |
| - |
25 |
| - |
26 |
| -## Solutions |
27 |
| - |
28 |
| - |
29 |
| -### Python3 |
30 |
| - |
31 |
| -```python |
32 |
| - |
33 |
| -``` |
34 |
| - |
35 |
| -### Java |
36 |
| - |
37 |
| -```java |
38 |
| - |
39 |
| -``` |
40 |
| - |
41 |
| -### ... |
42 |
| -``` |
43 |
| - |
44 |
| -``` |
| 1 | +# [10.09. Sorted Matrix Search](https://leetcode-cn.com/problems/sorted-matrix-search-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>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.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p><strong>Example:</strong></p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p>Given matrix:</p> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +<pre> |
| 17 | + |
| 18 | +[ |
| 19 | + |
| 20 | + [1, 4, 7, 11, 15], |
| 21 | + |
| 22 | + [2, 5, 8, 12, 19], |
| 23 | + |
| 24 | + [3, 6, 9, 16, 22], |
| 25 | + |
| 26 | + [10, 13, 14, 17, 24], |
| 27 | + |
| 28 | + [18, 21, 23, 26, 30] |
| 29 | + |
| 30 | +] |
| 31 | + |
| 32 | +</pre> |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +<p>Given target = 5, return <code>true.</code></p> |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +<p>Given target = 20, return <code>false.</code></p> |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Solutions |
| 46 | + |
| 47 | + |
| 48 | +### Python3 |
| 49 | + |
| 50 | +```python |
| 51 | +class Solution: |
| 52 | + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: |
| 53 | + if not matrix or not matrix[0]: |
| 54 | + return False |
| 55 | + rows, cols = len(matrix), len(matrix[0]) |
| 56 | + i, j = rows - 1, 0 |
| 57 | + while i >= 0 and j < cols: |
| 58 | + if matrix[i][j] == target: |
| 59 | + return True |
| 60 | + if matrix[i][j] > target: |
| 61 | + i -= 1 |
| 62 | + else: |
| 63 | + j += 1 |
| 64 | + return False |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +### Java |
| 69 | + |
| 70 | +```java |
| 71 | +class Solution { |
| 72 | + public boolean searchMatrix(int[][] matrix, int target) { |
| 73 | + if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + int rows = matrix.length, cols = matrix[0].length; |
| 77 | + int i = rows - 1, j = 0; |
| 78 | + while (i >= 0 && j < cols) { |
| 79 | + if (matrix[i][j] == target) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + if (matrix[i][j] > target) { |
| 83 | + --i; |
| 84 | + } else { |
| 85 | + ++j; |
| 86 | + } |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### ... |
| 94 | +``` |
| 95 | +
|
| 96 | +``` |
0 commit comments