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. 若给定target小于数组第一个元素,返回0;
  2. 若target大于最后一个元素,返回n

使用二分,结束循环条件应该是high-low=1的情况。

例如[1,3,5,6]中查找2,二分一次后,low=0,high=1,此时A[low]=1,A[high]=3。

若按照平常使用的二分查找就应该找不到元素exit了,但是要返回元素的值则需要再进一步处理,此时low+1,或high-1即为元素应该的位置。

下面代码里我把target等于边界值(数组第1个和第n个)的情况放到最后比较了。

 class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(A == NULL || n < )
return -;
if(target < A[])
return ;
if(target > A[n-])
return n; int low = ;
int high = n-; int mid = ; while(high-low>){
mid = low + (high - low)/;
if(A[mid] == target)
return mid;
else if(A[mid] > target)
high = mid;
else
low = mid;
} if(high-low ==)
if(A[low] == target)
return low;
if(A[high] == target)
return high;
else
return low+; }
};

Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置的更多相关文章

  1. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

  2. C语言-查找一个元素在数组中的位置

    #include<stdio.h> #include <stdlib.h> #include <time.h> int search(int key, int a[ ...

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

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

  4. [LeetCode][Java] Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  5. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  6. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  8. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

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

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

随机推荐

  1. SpringBoot整合JdbcTemplate连接Mysql

    import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...

  2. Jupyter Notebook & Spyder 常用快捷键

    Jupyter Notebook: 1. "shift + enter" notebook单元格内执行代码,并且自动跳至下一个的单元格 2. "tab" 自动补 ...

  3. 关于Matlab串口发送HEX格式字符

    终于想起来更新一下关于使用Matlab串口发送HEX格式字符.这个用法主要来自于我使用Matlab对机器人进行实时轨迹跟踪的绘制,由于底层限制,自己又不想在中间增加转换模块,就需要直接发送HEX格式指 ...

  4. jQuery $(document).ready()和JavaScript window.onload()事件的区别

    一. 在网上查了一下,发现$(document).ready()是在DOM树加载完成时触发,而window.onload()则是在整个页面全部加载完成时触发.下面是一些验证. var start=+n ...

  5. C 扩展库 - mysql API general outline

    Application programs should use this general outline for interacting with MySQL Initialize the MySQL ...

  6. js中字符串的替换

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...

  7. 公司Git实用记录

    一.git命令名词解释 1.添加/跟踪/暂存:添加到本地索引 git add 文件名 2.提交:提交到本地仓库 git commit -m '注释' 3.推送:将提交到本地仓库的所有更新提交到服务器 ...

  8. 使用HTML+CSS实现鼠标划过的二级菜单栏

    先上效果图: 1.鼠标没在上面 2.鼠标放在一级菜单上,展开二级菜单 3.鼠标放在二级菜单上 代码: (点击此处预览代码效果) <html> <head> <title& ...

  9. Varint数值压缩算法

    Varint 是一种紧凑的表示数字的方法.它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数.这能减少用来表示数字的字节数.比如对于 int32 类型的数字,一般需要 4 个 byte 来 ...

  10. 基于ASP.NET Core 创建 Web API

    使用 Visual Studio 创建项目. 文件->新建->项目,选择创建 ASP.NET Core Web 应用程序. 基于 ASP.NET Core 2.0 ,选择API,身份验证选 ...