-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path35-Search-Insert-Position.cpp
More file actions
32 lines (29 loc) · 915 Bytes
/
35-Search-Insert-Position.cpp
File metadata and controls
32 lines (29 loc) · 915 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 ans = 0;
for(int i = 0; i < nums.size(); i++){
if(nums[i] < target){
if(i == nums.size()-1){
ans = nums.size();
}
else if(target <= nums[i+1]){
ans = i+1;
}
else{
continue;
}
}
}
return ans;
}
};
/* 35. Search-Insert-Position.cpp
//////////////////////////////////////////////////
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.
Input: nums = [1,3,5,6], target = 5
Output: 2
https://leetcode.com/problems/search-insert-position/
//////////////////////////////////////////////////
*/