-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.cpp
More file actions
39 lines (34 loc) · 714 Bytes
/
35.cpp
File metadata and controls
39 lines (34 loc) · 714 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
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;
/*
存在目标值
不存在目标值
*/
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int left = 0;
int right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
return mid;
}
}
return left;
}
int main() {
vector<int> nums;
int target;
int ret;
nums = {1, 3, 5, 6};
// target = 5;
target = 2;
ret = searchInsert(nums, target);
cout << "ret:" << ret << endl;
return 0;
}