forked from soulmachine/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 97
/
chapSearching.tex
203 lines (153 loc) · 4.87 KB
/
chapSearching.tex
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
\chapter{查找}
\section{Search for a Range} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:search-for-a-range}
\subsubsection{描述}
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of $O(\log n)$.
If the target is not found in the array, return \code{[-1, -1]}.
For example,
Given \code{[5, 7, 7, 8, 8, 10]} and target value 8,
return \code{[3, 4]}.
\subsubsection{分析}
已经排好了序,用二分查找。
\subsubsection{代码}
\begin{Code}
// LeetCode, Search for a Range
// 偷懒的做法
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
const int l = distance(A, lower_bound(A, A + n, target));
const int u = distance(A, prev(upper_bound(A, A + n, target)));
if (A[l] != target) // not found
return vector<int> { -1, -1 };
else
return vector<int> { l, u };
}
};
\end{Code}
\begin{Code}
// LeetCode, Search for a Range
// 重新实现 lower_bound 和 upper_bound
class Solution {
public:
vector<int> searchRange (int A[], int n, int target) {
auto lower = lower_bound(A, A + n, target);
auto uppper = upper_bound(lower, A + n, target);
if (lower == A + n || *lower != target)
return vector<int> { -1, -1 };
else
return vector<int> {distance(A, lower), distance(A, prev(uppper))};
}
template<typename ForwardIterator, typename T>
ForwardIterator lower_bound (ForwardIterator first,
ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance(first, last) / 2);
if (value > *mid) first = ++mid;
else last = mid;
}
return first;
}
template<typename ForwardIterator, typename T>
ForwardIterator upper_bound (ForwardIterator first,
ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance (first, last) / 2);
if (value >= *mid) first = ++mid; // 与 lower_bound 仅此不同
else last = mid;
}
return first;
}
};
\end{Code}
\subsubsection{相关题目}
\begindot
\item Search Insert Position, 见 \S \ref{sec:search-insert-position}
\myenddot
\section{Search Insert Position} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:search-insert-position}
\subsubsection{描述}
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
\begin{Code}
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
\end{Code}
\subsubsection{分析}
即\fn{std::lower_bound()}。
\subsubsection{代码}
\begin{Code}
// LeetCode, Search Insert Position
// 重新实现 lower_bound
class Solution {
public:
int searchInsert(int A[], int n, int target) {
return lower_bound(A, A + n, target) - A;
}
template<typename ForwardIterator, typename T>
ForwardIterator lower_bound (ForwardIterator first,
ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance(first, last) / 2);
if (value > *mid) first = ++mid;
else last = mid;
}
return first;
}
};
\end{Code}
\subsubsection{相关题目}
\begindot
\item Search for a Range, 见 \S \ref{sec:search-for-a-range}
\myenddot
\section{Search a 2D Matrix} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\label{sec:search-a-2d-matrix}
\subsubsection{描述}
Write an efficient algorithm that searches for a value in an $m \times n$ matrix. This matrix has the following properties:
\begindot
\item Integers in each row are sorted from left to right.
\item The first integer of each row is greater than the last integer of the previous row.
\myenddot
For example, Consider the following matrix:
\begin{Code}
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
\end{Code}
Given \fn{target = 3}, return true.
\subsubsection{分析}
二分查找。
\subsubsection{代码}
\begin{Code}
// LeetCode, Search a 2D Matrix
class Solution {
public:
bool searchMatrix(const vector<vector<int>>& matrix, int target) {
const size_t m = matrix.size();
const size_t n = matrix.front().size();
int first = 0;
int last = m * n;
while (first < last) {
int mid = first + (last - first) / 2;
int value = matrix[mid / n][mid % n];
if (value == target)
return true;
else if (value < target)
first = mid + 1;
else
last = mid;
}
return false;
}
};
\end{Code}
\subsubsection{相关题目}
\begindot
\item 无
\myenddot