-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearch a 2d matrix II-Leetcode-AnujSoni.cpp
More file actions
38 lines (31 loc) · 1.11 KB
/
Search a 2d matrix II-Leetcode-AnujSoni.cpp
File metadata and controls
38 lines (31 loc) · 1.11 KB
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
35
36
37
38
/*
The idea is simple start from the top right corner of the matrix
1. if the target is smaller than the present element at (i,j)
then it is also smaller than all the elements in the column j (because matrix is sorted coulmn wise also)
therefore we change the coulumn i.e j--;
2. else if the target is greater than the present element at (i,j)
then it is also greater than all the elements in the row i (because matrix is sorted row wise also)
therefore we change the row i.e i++;
3. else return true because we have found the element
4. otherwise return false because target is not present
BELOW is the implementation of this illustration.
*/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if(m == 0)
return false;
int n = matrix[0].size();
int i=0,j=n-1;
while(i<m && j>=0){
if(target < matrix[i][j])
j--;
else if(target > matrix[i][j])
i++;
else
return true;
}
return false;
}
};