forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0035-search-insert-position.cpp
34 lines (28 loc) · 935 Bytes
/
0035-search-insert-position.cpp
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
/*
Given a sorted array of distinct integers 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.
Ex.
Input: nums = [1,3,5,6], target = 5
Output: 2
1.- Find the number in the middle of the vector.
2.- Takes a part (first or second), depending on whether or not the target is greater than the middel.
3.- Change the current left or right part.
3.- Do this process until the left exceeds the right.
Time: O(log n)
Space: O(1)
*/
classSolution {
public:
intsearchInsert(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
};