35. Search Insert Position
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.
Here are few examples.
[1, 3, 5, 6], 5 → 2
[1, 3, 5, 6], 2 → 1
[1, 3, 5, 6], 7 → 4
[1, 3, 5, 6], 0 → 0
题意:
给定一个排序数组和一个目标元素值,如果目标元素值在数组中,返回数组中的索引,如果不在数组中,返回其插入数组中的位置,并且插入后数组还是有序的。
思路:
方法一:
直接利用迭代器进行遍历数组。
1 | int searchInsert(vector<int>& nums, int target) { |
方法一:
利用折半查找。
1 | int searchInsert(vector<int>& nums, int target) { |
Java Code:
1 | class Solution { |