Skip to content

Commit 214d907

Browse files
committed
Fixed the chapter Searching
1 parent 432bee4 commit 214d907

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

C++/chapSearching.tex

+11-11
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ \subsubsection{使用STL}
2828
// 时间复杂度O(logn),空间复杂度O(1)
2929
class Solution {
3030
public:
31-
vector<int> searchRange(int A[], int n, int target) {
32-
const int l = distance(A, lower_bound(A, A + n, target));
33-
const int u = distance(A, prev(upper_bound(A, A + n, target)));
34-
if (A[l] != target) // not found
31+
vector<int> searchRange(vector<int>& nums, int target) {
32+
const int l = distance(nums.begin(), lower_bound(nums.begin(), nums.end(), target));
33+
const int u = distance(nums.begin(), prev(upper_bound(nums.begin(), nums.end(), target)));
34+
if (nums[l] != target) // not found
3535
return vector<int> { -1, -1 };
3636
else
3737
return vector<int> { l, u };
@@ -47,14 +47,14 @@ \subsubsection{重新实现 lower_bound 和 upper_bound}
4747
// 时间复杂度O(logn),空间复杂度O(1)
4848
class Solution {
4949
public:
50-
vector<int> searchRange (int A[], int n, int target) {
51-
auto lower = lower_bound(A, A + n, target);
52-
auto uppper = upper_bound(lower, A + n, target);
50+
vector<int> searchRange (vector<int>& nums, int target) {
51+
auto lower = lower_bound(nums.begin(), nums.end(), target);
52+
auto uppper = upper_bound(lower, nums.end(), target);
5353

54-
if (lower == A + n || *lower != target)
54+
if (lower == nums.end() || *lower != target)
5555
return vector<int> { -1, -1 };
5656
else
57-
return vector<int> {distance(A, lower), distance(A, prev(uppper))};
57+
return vector<int> {distance(nums.begin(), lower), distance(nums.begin(), prev(uppper))};
5858
}
5959

6060
template<typename ForwardIterator, typename T>
@@ -120,8 +120,8 @@ \subsubsection{代码}
120120
// 时间复杂度O(logn),空间复杂度O(1)
121121
class Solution {
122122
public:
123-
int searchInsert(int A[], int n, int target) {
124-
return lower_bound(A, A + n, target) - A;
123+
int searchInsert(vector<int>& nums, int target) {
124+
return distance(nums.begin(), lower_bound(nums.begin(), nums.end(), target));
125125
}
126126

127127
template<typename ForwardIterator, typename T>

0 commit comments

Comments
 (0)