-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path035_search-insert-position.h
More file actions
53 lines (44 loc) · 1.22 KB
/
035_search-insert-position.h
File metadata and controls
53 lines (44 loc) · 1.22 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <iostream>
#include <vector>
using namespace std;
/*
给出一个有序的数组和一个目标值,如果数组中存在该目标值,则返回该目标值的下标。
如果数组中不存在该目标值,则返回如果将该目标值插入这个数组应该插入的位置的下标
假设数组中没有重复项。
Given a sorted array 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.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
*/
class Solution_035
{
public:
int searchInsert(vector<int>& nums, int target)
{
return recursiveSearch(nums, target, 0, nums.size() - 1);
}
int recursiveSearch(vector<int>& nums, int target, int left, int right)
{
if (left >= right)
return nums[left] >= target ? left : left + 1;
int mid = left + (right - left) / 2;
if (nums[mid] > target)
return recursiveSearch(nums, target, left, mid);
else if (nums[mid] < target)
return recursiveSearch(nums, target, mid + 1, right);
else
return mid;
}
};