Skip to content

Commit 5e20581

Browse files
Added Leetcode Problem 240.
Problem : Search a 2D Matrix II URL : https://leetcode.com/problems/search-a-2d-matrix-ii/
1 parent 69da0ab commit 5e20581

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)