题目

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

分析

此题目与上一个题目相似都是源于二分查找的变形。

若序列中存在目标元素值,则直接返回其下标,若不存在则返回第一个大于它的元素的下标。仅仅须要在二分搜索算法中略微改动就可以。

AC代码

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (nums.size() == 0)
return 0;
else if (nums.size() == 1)
{
if (nums[0] >= target)
return 0;
else
return 1;
}
else{
return BinarySearch(nums, target);
}
} int BinarySearch(vector<int> & nums, int target)
{
int left = 0, right = nums.size() - 1; while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
{
if (mid == right || nums[mid + 1] > target)
return mid + 1;
else
left = mid + 1;
}
else{
if (mid == left || nums[mid - 1] < target)
return mid;
else
right = mid - 1;
}
}//while return -1;
}
};

GitHub測试程序源代码

LeetCode(35) Search Insert Position的更多相关文章

  1. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  2. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  3. LeetCode(81) Search in Rotated Array II

    题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  4. LeetCode(74) Search a 2D Matrix

    题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...

  5. LeetCode(34)Search for a Range

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

  6. LeetCode(33)Search in Rotated Sorted Array

    题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

  7. LeetCode(35):搜索插入位置

    Easy! 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1 ...

  8. Leetcode(35)-搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 这个题目很简单,因为它是给定的排序数组而且没有重 ...

  9. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

随机推荐

  1. VREP中的力触觉设备接口(CHAI3D)

    力反馈技术是一种新型的人机交互技术,它允许用户借助力反馈设备触碰.操纵计算机生成的虚拟环境中的物体,并感知物体的运动和相应的力反馈信息,实现人机力觉交互.虽然传统的鼠标.键盘.触摸屏等交互手段可以满足 ...

  2. LeetCode——Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. Android API之android.content.BroadcastReceiver

    android.content.BroadcastReceiver Base class for code that will receive intents sent by sendBroadcas ...

  4. Tensorflow异常集锦

    一.tensorflow checkpoint报错 在调用tf.train.Saver#save时,如果使用的路径是绝对路径,那么保存的checkpoint里面用的就是绝对路径:如果使用的是相对路径, ...

  5. fisheye Error occurred during initialization of VM Could not reserve enough space for object heap 问题解决!

    参考文章:https://answers.atlassian.com/questions/9397/not-enough-heap-space-to-run-fisheye fisheye下载好了之后 ...

  6. iOS配置SSO授权

    禁止/激活SSO授权 用于控制平台是否使用SSO方式进行授权(目前只支持新浪微博.Facebook.QQ空间.腾讯微博.人人网.Pocket.默认情况下是激活SSO授权方式.),代码如下: //激活S ...

  7. php使用wkhtmltopdf导出pdf

    参考:史上最强php生成pdf文件,html转pdf文件方法 http://biostall.com/wkhtmltopdf-add-header-footer-to-only-first-last- ...

  8. winetricks 用WineTricks令你的Wine更完整

    Linux下最有名的Windows环境模拟器就是WINE了.它提供了一个可以模拟WINDOWS环境的基本平台,在这上面你几乎可以运行任何你想运行的windows程序. 什么?你不相信?不要告诉我你的程 ...

  9. JVM调优总结(一)-- 堆和栈的基本概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...

  10. go interface 的坑

    一.概述 [root@node175 demo]# tree . ├── lib │   └── world.go ├── README └── server.go directory, files ...