@@ -43,17 +43,16 @@ class Solution:
43
43
def searchMatrix (self , matrix : List[List[int ]], target : int ) -> bool :
44
44
if not matrix or not matrix[0 ]:
45
45
return False
46
- rows, cols = len (matrix), len (matrix[0 ])
47
- i, j = rows - 1 , 0
48
- while i >= 0 and j < cols :
46
+ m, n = len (matrix), len (matrix[0 ])
47
+ i, j = m - 1 , 0
48
+ while i >= 0 and j < n :
49
49
if matrix[i][j] == target:
50
50
return True
51
51
if matrix[i][j] > target:
52
52
i -= 1
53
53
else :
54
54
j += 1
55
55
return False
56
-
57
56
```
58
57
59
58
### ** Java**
@@ -64,9 +63,9 @@ class Solution {
64
63
if (matrix == null || matrix. length == 0 || matrix[0 ] == null || matrix[0 ]. length == 0 ) {
65
64
return false ;
66
65
}
67
- int rows = matrix. length, cols = matrix[0 ]. length;
68
- int i = rows - 1 , j = 0 ;
69
- while (i >= 0 && j < cols ) {
66
+ int m = matrix. length, n = matrix[0 ]. length;
67
+ int i = m - 1 , j = 0 ;
68
+ while (i >= 0 && j < n ) {
70
69
if (matrix[i][j] == target) {
71
70
return true ;
72
71
}
@@ -81,6 +80,49 @@ class Solution {
81
80
}
82
81
```
83
82
83
+ ### ** C++**
84
+
85
+ ``` cpp
86
+ class Solution {
87
+ public:
88
+ bool searchMatrix(vector<vector<int >>& matrix, int target) {
89
+ if (matrix.size() == 0 || matrix[ 0] .size() == 0) return false;
90
+ int m = matrix.size(), n = matrix[ 0] .size();
91
+ int i = m - 1, j = 0;
92
+ while (i >= 0 && j < n)
93
+ {
94
+ if (matrix[ i] [ j ] == target) return true;
95
+ if (matrix[ i] [ j ] > target) --i;
96
+ else ++j;
97
+ }
98
+ return false;
99
+ }
100
+ };
101
+ ```
102
+
103
+ ### **Go**
104
+
105
+ ```go
106
+ func searchMatrix(matrix [][]int, target int) bool {
107
+ if len(matrix) == 0 || len(matrix[0]) == 0 {
108
+ return false
109
+ }
110
+ m, n := len(matrix), len(matrix[0])
111
+ i, j := m-1, 0
112
+ for i >= 0 && j < n {
113
+ if matrix[i][j] == target {
114
+ return true
115
+ }
116
+ if matrix[i][j] > target {
117
+ i--
118
+ } else {
119
+ j++
120
+ }
121
+ }
122
+ return false
123
+ }
124
+ ```
125
+
84
126
### ** ...**
85
127
86
128
```
0 commit comments