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