-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsearch_matrix.cpp
131 lines (116 loc) · 3.08 KB
/
search_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @Author: Chacha
* @Date: 2018-11-27 09:28:23
* @Last Modified by: Chacha
* @Last Modified time: 2021-03-26 18:06:46
*/
/***********************************************************************************
* Write an efficient algorithm that searches for a value in an m x n matrix.
* This matrix has the following properties:
* 1. Integers in each row are sorted from left to right.
* 2. The first integer of each row is greater than the last integer of the previous row.
*
* Example 1:
* Input:
* matrix = [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
* target = 3
* Output: true
*
* Example 2:
* matrix = [
* [1, 3, 5, 7],
* [10, 11, 16, 20],
* [23, 30, 34, 50]
* ]
* target = 13
* Output: false
*
* Source: https://leetcode.com/problems/search-a-2d-matrix/
************************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
bool searchMatrix1(vector<vector<int>> matrix, int target)
{
if (matrix.empty() || matrix[0].empty())
{
return false;
}
const int ROW = matrix.size(), COL = matrix[0].size();
int start = -1, end = ROW * COL, mid;
while (start + 1 < end)
{
mid = start + (end - start) / 2;
if (matrix[mid / COL][mid % COL] < target)
{
start = mid;
}
else
{
if (matrix[mid / COL][mid % COL] == target)
{
return true;
}
end = mid;
}
}
return false;
}
/***********************************************************************************
* Write an efficient algorithm that searches for a value in an m x n matrix,
* return the occurrence of it.This matrix has the following properties:
* 1. Integers in each row are sorted from left to right.
* 2. Integers in each column are sorted from up to bottom.
* 3. No duplicate integers in each row or column.
*
* Example:
* [1, 3, 5, 7],
* [2, 4, 7, 8],
* [3, 5, 9, 10]
*
* Given target = 3, return 2.
*
* Source: https://leetcode.com/problems/search-a-2d-matrix-ii/
************************************************************************************/
bool searchMatrix2(vector<vector<int>> &matrix, int target)
{
if (matrix.empty() || matrix[0].empty())
{
return false;
}
const int ROW = matrix.size();
const int COL = matrix[0].size();
int row = 0, col = COL - 1;
while (row < ROW && col >= 0)
{
if (matrix[row][col] == target)
{
return true;
}
else if (target < matrix[row][col])
{
--col;
}
else
{
++row;
}
}
return false;
}
int main()
{
int matrix[3][4] = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}};
vector<vector<int>> matrixVec(3, vector<int>(4));
int result1 = searchMatrix1(matrixVec, 10);
bool result2 = searchMatrix2(matrixVec, 34);
cout << "Result1 is " << result1 << "\n";
cout << "Result2 is " << result2 << endl;
}