-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchIn2DMatrix.cpp
More file actions
80 lines (67 loc) · 1.33 KB
/
SearchIn2DMatrix.cpp
File metadata and controls
80 lines (67 loc) · 1.33 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
// QUESTION: https://leetcode.com/problems/search-a-2d-matrix/
class MatrixSearcher
{
private:
const std::vector<std::vector<int>>& mat;
int rows;
int cols;
public:
MatrixSearcher(const std::vector<std::vector<int>>& m)
: mat(m)
{
rows = mat.size();
cols = mat[0].size();
}
bool find(int target)
{
int low = getLinIndex(0, 0);
int high = getLinIndex(mat.size() - 1, mat[0].size() - 1);
return binarySearch(low, high, target);
}
private:
bool binarySearch(int low, int high, int target)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
int midvalue = getValue(mid);
if (midvalue == target)
return true;
if (target > midvalue)
{
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
int getValue(int linIndex)
{
std::pair<int, int> coords = getXY(linIndex);
return mat[coords.first][coords.second];
}
int getLinIndex(int x, int y)
{
return cols*x + y;
}
std::pair<int, int> getXY(int linIndex)
{
return std::make_pair(linIndex / cols, linIndex % cols);
}
};
int main()
{
std::vector<std::vector<int>> mat{
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
int target = 3;
MatrixSearcher obj(mat);
bool result = obj.find(target);
std::cout << "Found status is " << result << std::endl;
return 0;
}