-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.cpp
More file actions
30 lines (28 loc) · 702 Bytes
/
Copy path35.cpp
File metadata and controls
30 lines (28 loc) · 702 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
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int meio = 0;
while(left <= right){
meio = (left + right) / 2;
if(nums[meio] == target){
return meio;
}else if(nums[meio] > target){
right = meio - 1;
}else{
left = meio + 1;
}
}
if(nums[meio] < target){
return meio + 1;
}else{
if(meio == 0){
return 0;
}else{
return meio;
}
}
return meio + 1;
}
};