Skip to content

Commit 78cf61d

Browse files
committed
240. Search a 2D Matrix II
1 parent 66e28f3 commit 78cf61d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

search-a-2d-matrix-ii.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Runtime: 55 ms
2+
class Solution {
3+
public:
4+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
5+
int i, j;
6+
7+
i = 0;
8+
if (matrix.size() < 1 || matrix[0].size() < 1)
9+
return 0;
10+
11+
j = matrix[0].size() - 1;
12+
while (i < matrix.size() && j >= 0)
13+
{
14+
if (target == matrix[i][j])
15+
return 1;
16+
17+
else if (target < matrix[i][j])
18+
j--;
19+
else
20+
i++;
21+
}
22+
return 0;
23+
}
24+
};

0 commit comments

Comments
 (0)