forked from dscmsit/Problem-Solving-in-any-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch2DMatrix2.cpp
More file actions
34 lines (30 loc) · 720 Bytes
/
Search2DMatrix2.cpp
File metadata and controls
34 lines (30 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 240. Leetcode {MEDIUM} [Binary Search , Array , Matrix]
#include <bits/stdc++.h>
using namespace std;
// Used the property of sorted matrix order for carrying out the searching of elements!
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
int n = matrix.size();
int m = matrix[0].size();
int i = 0, j = m - 1;
while (i < n && j >= 0)
{
if (matrix[i][j] == target)
{
return true;
}
if (matrix[i][j] > target)
{
j--;
}
else
{
i++;
}
}
return false;
}
};