-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_2d_matrix.cpp
83 lines (77 loc) · 1.98 KB
/
search_2d_matrix.cpp
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
81
82
83
// =====================================================================================
//
// Filename: search_2d_matrix.cpp
//
// Description: 74. Search a 2D Matrix. Write an efficient algorithm that searches
// for a value in an m x n matrix.
//
// Version: 1.0
// Created: 10/17/2019 07:56:02 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <vector>
using std::vector;
class Solution
{
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int left = 0;
int right = matrix.size() - 1;
while (left < right)
{
int middle = (left + right + 1) / 2;
if (target < matrix[middle][0])
{
right = middle - 1;
}
else
{
left = middle;
}
}
if (left != right)
{
return false;
}
// left == right
const auto& values = matrix[left];
left = 0;
right = values.size() - 1;
while (left < right)
{
int middle = (left + right + 1) / 2;
if (target < values[middle])
{
right = middle - 1;
}
else if (target > values[middle])
{
left = middle + 1;
}
else
{
return true;
}
}
return (left < values.size() && values[left] == target);
}
};
int main(int argc, char* argv[])
{
vector<vector<int>> matrix = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50},
};
int value = 3;
bool found = Solution().searchMatrix(matrix, value);
printf("Found %d? %s\n", value, (found ? "Yes" : "No"));
return 0;
}