-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchA2DMatrixII.cpp
More file actions
35 lines (34 loc) · 900 Bytes
/
SearchA2DMatrixII.cpp
File metadata and controls
35 lines (34 loc) · 900 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
35
class Solution {
public:
/**
* @param matrix: A list of lists of integers
* @param target: An integer you want to search in matrix
* @return: An integer indicate the total occurrence of target in the given matrix
*/
int searchMatrix(vector<vector<int>> &matrix, int target) {
if (matrix.empty() || matrix[0].empty())
return 0;
// write your code here
int total = 0;
int row = matrix.size() - 1;
int col = 0;
while (row >= 0 && col < matrix[0].size())
{
if (matrix[row][col] > target)
{
row--;
}
else if (matrix[row][col] < target)
{
col++;
}
else
{
total++;
row--;
col++;
}
}
return total;
}
};